首页 电脑 电脑学堂 查看内容

sqlite

2012-8-27 11:22 872 0

摘要:   一、进入数据库  命令行输入下列命令进入数据库  1.adb  2.adb shell  3.cd data  4.cd data  5.cd [包名]  6.创建数据库后  7.cd datab...
关键词: String 数据库 values 方法 whereClause 更新 table 数据 ContentValues 语句

  一、进入数据库  命令行输入下列命令进入数据库  1.adb  2.adb shell  3.cd data  4.cd data  5.cd [包名]  6.创建数据库后  7.cd databases  8.sqlite3 [数据库名]  即数据库存放在/data/data/[包名]/databases/路径下  常用命令  .schema     查看当前数据库中有哪些表  二、知识点  1、public void execSQL(String sql);  执行单个SQL语句,但不能执行SELECT和其他有返回值的SQL语句;  2、SQL语句  语句:create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)  说明:tabname——表名,col1,col2——字段名(即列名),not null——该字段非空,primary key——主键(某条记录的唯一标识符)。  3、public long insert (String table, String nullColumnHack, ContentValues values)  注:插入记录,table——表名,nullColumnHack——强行插入null值的字段(列名),values——一行记录的数据。  4、public int update (String table, ContentValues values, String whereClause, String[] whereArgs)  注:更新数据库中数据;table——想要更新的表名,values——想要更新的数据,whereClause——满足该whereClause子句的记录将会被更新,whereArgs——为whereClause传入参数。(可理解为:在表table中,根据whereClause和whereArgs查找出数据,更新为values)  如:更新person_inf表中所有主键大于20的人的人名,  ContentValues values = new ContentValues();  values.put(“name”,“新人名”);  int result = db.update(“person_inf”,values,”_id > ?”, new Integer[]{20});  5、public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)  注:查询,详情见帮助。  6.SQLiteOpenHelper类中的onCreate方法  public abstract void onCreate (SQLiteDatabase db);  当调用SQliteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取用于操作数据库的SQLiteDatabase实例时,如果数据库不存在,Android系统会自动生成一个数据库,接着调用onCreta方法,onCreate方法在初次生成数据库时才被调用。  即:getWritableDatabase()、getReadableDatabase()在获取实例时,数据库不存在则创建,存在则不创建;而onCreate方法是第一次创建时自动调用的方法。因此,可重写该方法初始化数据表结构及数据等。
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部