SqliteDatabase的基本使用
来源:互联网 发布:域名删除期查询 编辑:程序博客网 时间:2024/05/13 18:50
SQLiteDatabase.openOrCreateDatabase(getFilesDir().getPath()+"/mycursor.db",null),此语句用于返回一个SQLiteDatabase实例。
建表语句:db.execSQ("create table if not exists myTable (_id integer primary key autoincrement,
news_title varchar(255),news_content varchar(255))");或者去掉if not exists,
数据库的类型有NULL,TEXT,INTEGER,REAL(浮点型),BOLB(大二进制对象)。在实际使用时也可以用char,varchar,将其转为五中类型。
数据库在存储时不分类型,上面语句等价于db.execSQ("create table if not exists myTable (_id integer primary key autoincrement,news_title ,news_content )"),但声明为
integer primary key只能存储64位的整数类型。
插入语句:1)db.execSQL(insert into myTable values(null,1212,'孙哈士奇'));
2)db.insert("myTable",null,contentvalue);
删除语句:myDb.execSQL(“delete from myTable ”);删除表但不释放空间,所以可以恢复已经删除的数据查询语句:1)db.query("myTable",new String[]{"news_title","news_content"},"_id<=10",null,null,null,null);
用于查询_id小于10的数据;
2)db.query("myTable",new String[]{"_id","news_title","news_content"},"news_content like ?",new String[]{"孙%"},null,null,null);
查询news_content以孙开头的所有数据,将%换为_表示查询孙某,__表示查询孙某某
关闭数据库: 数据库是一种资源,所以在activity销毁时得关闭数据库,常用语句为if (db != null&&db.isOpen()) { db.close();}
new SimpleCursorAdapter(this,R.layout.cursorlayout,c,new String[]{"news_title","news_content"}
,new int[]{R.id.textView,R.id.textView2}, CursorAdapter,cursor,FLAG_REGISTER_CONTENT_OBSERVER)。cursor的列中得包含_id。
ArrayList<HashMap<String,String>> result=new ArrayList<>();HashMap<String,String> map=new HashMap<>();map.put("word","haha”);map.put("detail","haha”);result.add(map);
new SimpleAdapter(this,result,R.layout.cursorlayout,new String[]{"word","detail"},new int[]{R.id.textView,R.id.textView2});beginTransaction()开始一个事务,endTransaction()结束一个事务-到底提交事务环视回滚事务取决于数据库是否调用setTransactionSuccessful()。调用则提交事务,不调用则回滚事务。SQLiteOpenHelper用于帮助数据库的操作,onCreate()用于数据库第一次建立时调用,onUpgrade()当数据库的新版本大于旧版本时调用。
getWritableDatabase()和getReadableDatabase()时调用SQLiteOpenHelper的onCreate,返回的数据库是一个单例。来弄个这之间的区别是getWritableDatabase
以写的方式打开数据库。当数据库的磁盘满时发生异常。getReadableDatabase以先读写的方式打开数据库。若磁盘满时,以写的方式打开数据库会发生异常,这是会以读的方式打开数据库。
0 0
- SQLiteDataBase的基本使用
- SqliteDatabase的基本使用
- SQLiteDatabase 基本使用
- Android SQLiteDatabase的使用
- SQLiteDataBase数据库的使用
- SQLiteDatabase的使用
- Android SQLiteDatabase的使用
- SQLiteDatabase数据库的使用
- Android SQLiteDatabase的使用
- android sqlitedatabase的使用
- sqlitedatabase的使用
- Android 数据库SQLiteDatabase的使用
- Android 数据库SQLiteDatabase的使用
- Android 数据库SQLiteDatabase的使用!!
- 探讨SQLiteDatabase的优雅使用
- android 中SQLiteDatabase的使用
- Android 数据库SQLiteDatabase的使用!!
- Android SQLiteDatabase的使用模板
- shell脚本启动android模拟器
- android7.0多窗口横屏模式下statusbar有条纹
- 使用 Eclipse Memory Analyzer 进行堆转储文件分析 Eclipse Memory Analyzer(MAT)
- 获取servlet API的方法------ActionContext VS sessionAware
- 微信公众号获取access_token
- SqliteDatabase的基本使用
- 一坨博弈问题的总结
- 关键字(register、static、consit、extern、typedef)
- [编程题]按字节截取字符串
- 杭电1229题还是A+B
- sql优化
- 大数据Spark “蘑菇云”行动第103课:Hive源码大师之路第一步:Hive源码思考和解析初体验
- 使用LayoutTransition实现布局变化时的动画
- 十分钟快速了解Java守护线程