您的位置:首页 > 移动开发 > Android开发

【Android】SQLite 数据库基本操作

2016-07-07 18:14 351 查看
1.使用DBHelper实现数据库的打开与创建

public class DBHelper extends SQLiteOpenHelper{
private final static String DB_NAME="notecontent.db";
private final static int DB_version=2;
public DBHelper(Context context){
super(context,DB_NAME,null,DB_version);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE IF NOT EXISTS notecontent"+
"(_id INTEGER PRIMARY KEY AUTOINCREMENT,date VARCHAR,detail NVARCHAR,"+
"recordurl VARCHAR,picurl VARCHAR,barcodeurl VARCHAR)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}


DBHelper继承SQLiteOpenHelper并必须实现onCreate与onUpgrade方法,使用
_id INTEGER PRIMARY KEY AUTOINCREMENT
在SQLite中创建自增id,这个自增id可以方便我们对表的行管理。

2.使用DBManager实现数据库的各种操作

public class DBManager {
private DBHelper helper;
private SQLiteDatabase db;

public DBManager(Context context){
helper=new DBHelper(context);
db=helper.getWritableDatabase();
}

public void add(NoteContent noteContent){
db.execSQL("INSERT INTO notecontent VALUES(null,?,?,?,?,?)", new Object[]{noteContent.date, noteContent.detail, noteContent.record_url, noteContent.pic_url, noteContent.barcode_url});

}

public void update(int key_id,NoteContent noteContent){
db.execSQL("UPDATE notecontent SET date=?,detail=?,recordurl=?,picurl=?,barcodeurl=? WHERE _id=?"
, new Object[]{noteContent.date, noteContent.detail, noteContent.record_url, noteContent.pic_url, noteContent.barcode_url,key_id});
}

public void delete(int key_id){

db.execSQL("DELETE FROM notecontent WHERE _id=?",new Object[]{key_id});
}

public List<NoteContent> query() {
ArrayList<NoteContent> notelists = new ArrayList<NoteContent>();
Cursor c = queryTheCursor();
while (c.moveToNext()) {
NoteContent noteitem = new NoteContent();
noteitem.date=c.getString(c.getColumnIndex("date"));
noteitem.detail=c.getString(c.getColumnIndex("detail"));
noteitem.record_url=c.getString(c.getColumnIndex("recordurl"));
noteitem.pic_url=c.getString(c.getColumnIndex("picurl"));
noteitem.barcode_url=c.getString(c.getColumnIndex("barcodeurl"));
noteitem.id=Integer.parseInt(c.getString(c.getColumnIndex("_id")));
notelists.add(noteitem);
}
c.close();
return notelists;
}
public Cursor queryTheCursor() {
Cursor c = db.rawQuery("SELECT * FROM noteContent", null);
return c;
}

public void closeDB() {
db.close();
}
}


使用占用符?在SQL语句当中替换所需的变量

"INSERT INTO notecontent VALUES(null,?,?,?,?,?)", new Object[]{noteContent.date, noteContent.detail, noteContent.record_url, noteContent.pic_url, noteContent.barcode_url});


自增id可以用null代替,且之后的占用符所对应的内容,用一个object数据存储,并要与之前的key值对应,这里因为全映射,所以notecontent后面对应的key值省略了。

使用Cursor类来对光标进行控制,
c.moveToNext()
改变光标位置进行行访问,用
c.getColumnIndex()
来访问列值,括号内是String key。

若要对数据库进行操作,先
mgr= new DBManager(this);
之后对mgr操作即可。

3.注意在Androidmainfest中注册读写权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


4.Android Studio中查看database的方法

打开Android Device Monitor 的file explorer



路径一般是data/data/com.test(你的包名)/database/*.db

然后点



导出**.db文件 之后可以用SQL可视化工具显示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android sqlite 数据库