Android下的數(shù)據(jù)庫
Sqlite數(shù)據(jù)庫, 移動平臺輕量級嵌入式的數(shù)據(jù)庫,一般用于IOS,Android等移動平臺
如何創(chuàng)建
創(chuàng)建文件
//新建一個文件對象
File file = new File("文件名");
//輸出流
FileOutputStream fos = new FileOutputStream(file);
fos.write(".........".getBytes());
創(chuàng)建數(shù)據(jù)庫
//創(chuàng)建數(shù)據(jù)庫對象
MyDBOpenHelper helper = new MyDBOpenHelper(this);
//必須通過幫助類獲取一個數(shù)據(jù)庫文件,只有獲取了可寫教沾、可讀的數(shù)據(jù)庫,數(shù)據(jù)庫文件才會被創(chuàng)建
helper.getWritableDatabase();
如何定義數(shù)據(jù)庫的表結(jié)構(gòu)
數(shù)據(jù)庫創(chuàng)建的幫助類SQLiteOpenHelper里面,有兩個重要的方法
- onCreate() 數(shù)據(jù)庫第一次被創(chuàng)建的時候調(diào)用的方法肋殴,數(shù)據(jù)庫表結(jié)構(gòu)的初始化
- onUpgrade() 數(shù)據(jù)庫的版本號增加的時候調(diào)用的方法孝治,如果舊數(shù)據(jù)庫表結(jié)構(gòu)定義不合理,可修改數(shù)據(jù)庫的表結(jié)構(gòu)
在onCreate()和onUpgrade()方法里面都有一個參數(shù) db ,代表當(dāng)前的數(shù)據(jù)庫,
得到數(shù)據(jù)庫,執(zhí)行sql語句
db.execSQL("create table contactinfo
(id integer primary key autoincrement, text varchar(10), num varchar(10))");
陶喆《似曾相識》...........
晚安!