1.數(shù)據(jù)庫的管理類
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
/**
* context:上下文
* name:數(shù)據(jù)庫名字
* factory:目的是創(chuàng)建游標(biāo)(游動光標(biāo))對象府框,默認(rèn)用null
* version:數(shù)據(jù)庫版本扁达,從1開始遞增傻挂,不能減
* */
public MyOpenHelper(Context context) {
super(context, "i01.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {//數(shù)據(jù)庫第一次創(chuàng)建的時候調(diào)用
/**適合初始化表結(jié)構(gòu),用sql語句創(chuàng)建表
* 示例:創(chuàng)建一個兩列的表害淤,第一列為id,第二列為name
* id 一般以下劃線開頭“_id”
* interger 是數(shù)據(jù)類型
* primary key 指定id為主鍵
* autoincrement 自增長
* varchar(20) name不超過20個字符
* 注意:sqlite底層都將類型轉(zhuǎn)化為string類型,但為了嚴(yán)謹(jǐn)咱士,這里還是指定類型
*/
// db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20))");
//初始化一個三列的表
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//當(dāng)數(shù)據(jù)庫版本升級時調(diào)用
/**適合更新表結(jié)構(gòu)
* 示例:給表增加一列phone
* varchar(20) phone 不超過20個字符
* */
db.execSQL("alter table info add phone varchar(20)");
System.out.println("新版本數(shù)據(jù)庫:"+newVersion+"舊版本:"+oldVersion);
}
}
2.增刪改查操作
public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOpenHelper = new MyOpenHelper(getApplicationContext());
// //打開一個數(shù)據(jù)庫(如果不存在就創(chuàng)建)
// SQLiteDatabase writableDatabase = myOpenHelper.getWritableDatabase();
// //打開一個數(shù)據(jù)庫(如果不存在就創(chuàng)建)立由,跟上面的區(qū)別是,如果磁盤滿了序厉,返回一個只讀的數(shù)據(jù)庫
//// SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
}
public void click1(View v) {//點擊按鈕增加一條記錄
//獲取數(shù)據(jù)庫對象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//執(zhí)行增加一條的sql語句
db.execSQL("insert into info(name,phone) values(?,?)",new Object[]{"張三","13111112222"});
//數(shù)據(jù)庫用完需要關(guān)閉
db.close();
}
public void click2(View v) {//刪除一條記錄
//獲取數(shù)據(jù)庫對象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//執(zhí)行刪除一條的sql語句
db.execSQL("delete from info where name=?",new Object[]{"張三"});
//數(shù)據(jù)庫用完需要關(guān)閉
db.close();
}
public void click3(View v) {//修改一條記錄
//獲取數(shù)據(jù)庫對象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//執(zhí)行修改一條的sql語句
db.execSQL("update info set phone=? where name=?",new Object[]{"1871111","張三"});
//數(shù)據(jù)庫用完需要關(guān)閉
db.close();
}
public void click4(View v) {//查詢一條記錄
//獲取數(shù)據(jù)庫對象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//執(zhí)行查詢一條的sql語句(返回的是一個結(jié)果集锐膜,后面是占位符)
Cursor cursor = db.rawQuery("select * from info", null);//查詢所有數(shù)據(jù)
if (cursor!=null && cursor.getCount()>0) {//查詢結(jié)果不為空,且個數(shù)大于0
while (cursor.moveToNext()) {//循環(huán)弛房,每次都取下一個道盏,取不到則結(jié)束
//取值,參數(shù)為列的索引文捶,從0開始
String name = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("name:"+name+" phone:"+phone);
}
}
//數(shù)據(jù)庫用完需要關(guān)閉
db.close();
}
}