安卓笔记——SQLite数据库
Posted _23_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓笔记——SQLite数据库相关的知识,希望对你有一定的参考价值。
小提示:查看数据库中的内容可以直接到
查看打印情况到 Run
是什么?
简单来说,是指手机里的存储数据库
存储当前用户的一些数据
包括什么?
当前用户的聊天记录,用户名。数据缓存
特点
存储数据量比较少,占用的内存也比较小
第一种操作
插入数据
1、写完 activity_main.xml界面后,创建一个工具类
代码:
package com.hnucm.c201901020110;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class mysqlLiteHelper extends SQLiteOpenHelper {
public MySQLLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//创建表
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),age integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//修改表
}
}
2、在 MainActivity.java 中写主要逻辑
package com.hnucm.c201901020110;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//增加数据
MySQLLiteHelper mySQLLiteHelper = new MySQLLiteHelper(MainActivity.this,"test.db",null,1);
SQLiteDatabase db = mySQLLiteHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username","admin123");
contentValues.put("password","123456");
contentValues.put("age",20);
db.insert("user",null,contentValues);
}
});
}
}
删除操作
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除数据
MySQLLiteHelper mySQLLiteHelper = new MySQLLiteHelper(MainActivity.this,"test.db",null,1);
SQLiteDatabase db = mySQLLiteHelper.getWritableDatabase();
//表名,条件,
db.delete("user","id=? and username = ?",new String[]{"1","admin123"});
}
});
查询操作
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//查询数据
MySQLLiteHelper mySQLLiteHelper = new MySQLLiteHelper(MainActivity.this,"test.db",null,1);
SQLiteDatabase db = mySQLLiteHelper.getWritableDatabase();
//表名, ,条件,
Cursor cursor = db.query("user",null,"username = ?",new String[]{"admin123"},null,null,null);
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String username = cursor.getString(cursor.getColumnIndex("username"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i("Mainactivity", "result: id=" + id + " username: " + username + " age:" + age);
}
}
});
另一种对数据库的操作
插入数据
使用框架
1、创建一个对象的类
2、增加依赖包 @Entity
package com.hnucm.c201901020110;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "username")
public String username;
@ColumnInfo(name = "password")
public String password;
@ColumnInfo(name = "age")
public int age;
}
3、新建一个工具接口 UaerDao
package com.hnucm.c201901020110;
import androidx.room.Dao;
import androidx.room.Insert;
//数据库操作类 对表进行增删改查方法
@Dao
public interface UserDao {
@Insert
void insertUer(User... users);
}
4、新建一个工具类
package com.hnucm.c201901020110;
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {User.class},version = 1,exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {
public abstract UserDao getUserDao();
}
5、在 MainActivity.java 中写主要逻辑
//使用框架
UserDatabase userDatabase;
UserDao userDao;
//使用框架
userDatabase = Room.databaseBuilder(this,UserDatabase.class,"myuser")
.allowMainThreadQueries().build();
userDao = userDatabase.getUserDao();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//增加数据
User user = new User();
user.age = 20;
user.username = "admin1";
user.password = "12345678";
userDao.insertUer(user);
}
});
删除数据
1、在工具接口 UaerDao 中加入
@Delete
void deleteUser(User... users);
2、在 MainActivity.java 中写主要逻辑
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除数据
User user = new User();
user.id = 1;
userDao.deleteUser(user);
}
});
查询操作
1、在工具接口 UaerDao 中加入
@Query("SELECT * FROM user")
List<User> findAllUser();
2、在 MainActivity.java 中写主要逻辑
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//查询数据
List<User> userList = userDao.findAllUser();
for (User user : userList){
Log.i("","userid ="+ user.id + " name:"+user.username );
}
}
});
查询带条件的数据
以上是关于安卓笔记——SQLite数据库的主要内容,如果未能解决你的问题,请参考以下文章