【Android】SQLite 数据库基本操作
来源:互联网 发布:淘宝金牌卖家怎么申请 编辑:程序博客网 时间:2024/05/29 11:44
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可视化工具显示
0 0
- android数据库sqlite基本操作。
- 【Android】SQLite 数据库基本操作
- Android SQLite数据库基本操作
- android SQLite数据库基本操作
- android SQLite数据库基本操作示例
- android SQLite数据库基本操作示例
- Android的SQLite数据库基本操作
- Android操作SQLite数据库基本模式
- Android 之SQLite数据库基本操作
- sqlite数据库基本操作
- SQLite数据库基本操作
- sqlite数据库基本操作
- SQLite数据库基本操作
- 数据库sqlite基本操作
- SQLite数据库基本操作
- sqlite 数据库基本操作
- SQLite数据库基本操作
- 数据库 SQLite数据库基本操作
- SDKD 2016 Summer Single Contest #02.A
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
- [NOIP1999]拦截导弹
- 声学
- 1326. Window (Standard IO)
- 【Android】SQLite 数据库基本操作
- ACM中的博弈论入门(二) POJ 2960 SG 函数的应用
- 机器学习 --- 迭代决策树(GBDT)
- 最少拦截系统
- Length of Last Word
- Java之流
- xstream 别名的用法
- 在Xcode中使用Git进行源码版本控制
- PHP实现双向链表并排序 -- 会员排名演示