我们在进行APP开发的时候,经常会需要将各种数据保存起来,我们可以使用共享参数,但是那种方式保存的数据格式较为单一,所以我们还是要用到数据库保存更加丰富的数据。
Android中用的是内置的SQLite数据库,因为是内置的所以不用像MySQL需要配置,但为了方便,我还是推荐像我一样的菜鸟用可视化工具来进行数据管理,同时,也更加方便编程查错。
1: Android编程时,使用数据库保存数据发的步骤
1.1: 创建DatabaseHelper类
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
public static String CREATE_TABLE_LV ="create table if not exists letters_aa (" +
"title string," +
"text string," +
"file string," +
"date string)";
public static String CREATE_TABLE_LY="create table if not exists letters_bb(" +
"title string," +
"text string," +
"file string,"+
"date string)";
public static String CREATE_TABLE_SY="create table if not exists letters_cc("+
"title string," +
"text string," +
"file string," +
"date string)";
public static String CREATE_TEABLE_USER="create table if not exists letters_user1(" +
"account string," +
"pass string," +
"e_mail string," +
"shoucang string," +
"status string," +
"cheking string )";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
this.context =context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_LV);
db.execSQL(CREATE_TABLE_LY);
}
public void create_abc(SQLiteDatabase db){
db.execSQL(CREATE_TABLE_SY);
db.execSQL(CREATE_TABLE_LY);
db.execSQL(CREATE_TABLE_LV);
db.execSQL(CREATE_TEABLE_USER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这个帮助类中包括对数据库对象中表的操作(创建,修改,删除等)
public static String CREATE_TEABLE_USER="create table if not exists letters_user1(" +
"account string," +
"pass string," +
"e_mail string," +
"shoucang string," +
"status string," +
"cheking string )";
以及对数据库的初始化。
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
this.context =context;
}
1.2:数据库对象的获取
myDatabaseHelper =new MyDatabaseHelper(write_letter_activity.this,"test_02",null,1);
db =myDatabaseHelper.getWritableDatabase();
这里是获取可以写的数据库,如果只是从数据库读数据,就是getReadableDatabase()
拿到数据库后就可以进行数据的增删查改了(创建表的操作在onCreate方法中)
2,SQLite数据可视化的软件—SQLiteStudio使用步骤
2.1: 下载SQLiteStudio
自提链接:https://pan.baidu.com/s/1e_30zUvEAeoflhb_TKS-Cw
提取码:zie8
2.2: 在SQLiteStudio中打开项目中的数据库文件即可
2.2.1:将数据库文件从手机导出到电脑指定目录
数据线没带就不截图给你们看了,可以百度到步骤。
2.2.2:打开文件
更改这个小设置(填坑)。