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
原创粉丝点击