android——SQLite数据库存储(创建)

  Android 专门提供了SQLiteOpenHelper帮助类,借助这个类就可以非常简单的对数据库进行创建和升级。

  首先SQLiteOpenHelper是一个抽象类,在使用的时候需要创建一个自己的帮助类去继承它。

  SQLiteOpenHelper中有两个抽象方法onCreate()和onUpgrade(),在创建自己的帮助类的时候必须重写这两个方法,在这两个方法中分别实现创建、升级数据库的逻辑。 

  SQLiteOpenHelper中有两个非常重要的实例方法getWritableDatabase()和getReadableDatabase(),这两个方法都可以创建或打开一个现有的数据库(如果数据库已存在就是打开,不存在就会创建一个新的),

  SQLiteOpenHelper的构造方法有四个参数,第一个参数是Context,第二个是数据库的名字,第三个是允许我们查询数据的时候返回的一个自定义的Cursor,一般传入null即可,第四个是版本号。

例子:

  创建一个名字为Bookstore的数据库,在数据库中建一张Book表,表中有id(主键)、作者,价格、页数、书名。Book表的建表语句如下:

1 create table Book (
2     id integer primary key autoincrement,
3     author text,
4     price real,
5     pages integer,
6     name text)

  SQL的数据类型有:integer表示整型,real表示浮点型,text表示文本型,blob表示二进制型,primary key 将id设为主键,autoincrement关键字表示id 列是自增长的。

  在代码中,需要执行这个语句,新建一个MyDatabaseHelper类继承自SQLiteOpenhHelper,代码如下:

 1 public class MyDatabaseHelper extends SQLiteOpenHelper {
 2 
 3     //把定义SQL建表语句成字符串常量
 4     public static final String CREATE_BOOK = "create table Book("
 5             +"id integer primary key autoincrement,"
 6             +"author text,"
 7             +"price real,"
 8             +"pages integer,"
 9             +"name text)";
10 
11     private Context mContext;
12 
13     //构造方法
14     public  MyDatabaseHelper(Context context,String name,
15                              SQLiteDatabase.CursorFactory factory,int version){
16         super(context,name,factory,version);
17         mContext = context;
18     }
19 
20     //建表
21     @Override
22     public void onCreate(SQLiteDatabase db) {
23         db.execSQL(CREATE_BOOK);
24         Toast.makeText(mContext,"数据库创建成功",Toast.LENGTH_SHORT).show();
25     }
26 
27     @Override
28     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
29 
30     }
31 }

  把定义SQL建表语句成字符串常量,然后在onCreate()中调用SQLiteDatabase的db.execSQL()方法去执行,使用Toast提醒创建成功。

  然后在布局文件中加入一个按钮:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <Button
 8         android:id="@+id/creat_database"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="建立数据库"/>
12 
13 </LinearLayout>

  最后修改MainActivity的代码

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private MyDatabaseHelper dbHelper;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9 
10         //创建帮助类的实例
11         dbHelper = new MyDatabaseHelper(MainActivity.this,"BookStore.db",null,1);
12 
13         //注册按钮
14         Button creatDatabase = (Button) findViewById(R.id.creat_database);
15 
16         //按钮响应
17         creatDatabase.setOnClickListener(new View.OnClickListener() {
18             @Override
19             public void onClick(View view) {
20                 dbHelper.getWritableDatabase();
21             }
22         });
23 
24     }
25 }

  在onCreate()中构建了一个MyDatabaseHelper的对象dbHelper,并命名为BookStore.db,版本号1,通过按钮的点击事件去调用dbHelper的getWritableDatabase()方法,去创建数据库。

  结果如下图所示:

原文地址:https://www.cnblogs.com/xxbbtt/p/7346406.html