介绍

  SQLite是安卓内置的数据库,支持NULL,INTEGER,REAL,TEXT,BLOB

 

使用

  首先记住一点,对于where条件,是带有占位符的。 -----》 where id = ? and  username = ? -----》 "id = ? and username = ?" , new String [] { id , username }

  SQLite主要五个方法:

    long insert ( String table , String nullColumnHack , ContentValues values  )

      参数:

        String table:表名

        String nullColumHack:未知

        ContentValues values:包含字段名称和字段值.

      返回值:long

      返回值意义:插入条数

      作用:先数据库中表table插入数据

 

     Cursor query( String table , String [] columns ,String selection , String [] selectionArgs , String groupBy , String having , String orderBy )

      参数:

        String table:表名

        String [] columns : 查询字段

        String selection:查询条件

        String [] selectionArgs:查询条件赋值

        其它自己看,不需要就null

      返回值:Cursor

      返回值意义:类似ResultSet的Cursor

      作用:查询数据库里的数据,获得游标

 

      int Update( String table ,ContentValues values , String whereClause , String [] whereArg )

        参数:

          String table:表名

          ContentValues contentvalues:字段名和字段值

          String whereClause:条件语句

          String [] whereArg:条件语句赋值占位符

        返回值:int

        返回值意义:修改多少条语句

        作用:修改表内容

      

      int delete( String table ,String whereClause , String [] whereArg)

        参数:

          String table : 表名

          String whereClause:条件

          String[] whereArg:占位符赋值

        返回值:int

        返回值意义:删除多少条记录

        作用:删除表中的数据

 

      void execSQL(String sql )

        参数:

          String sql:sql语句

        返回值:无

        返回值意义:无

        作用:执行sql语句

 

SQLite主要使用就是这五种方法,其它都是简单的要死

 

SQLiteOpenHelper --- 创库创表 --  在构造方法内创建数据库,在onCreate() 方法中使用db创建数据表

SQLiteDataBase通过 SQLiteOpenHelper.getReadxxxxx()获取数据库。

 

使用:



class MyListener implements View.OnClickListener{

@Override
public void onClick(View v) {
SQLiteOpenHelperDemo sqLiteOpenHelperDemo = new SQLiteOpenHelperDemo(SQLiteDemo.this,null,null,1);
SQLiteDatabase db = sqLiteOpenHelperDemo.getReadableDatabase();
switch ( v.getId() ){
case R.id.btn_write:
String content = edt_content.getText().toString();
ContentValues contentValues = new ContentValues();
contentValues.put("uname",edt_content.getText().toString());
db.insert("user",null,contentValues);
Toast.makeText(SQLiteDemo.this,"写入完成",Toast.LENGTH_SHORT).show();
break;

case R.id.btn_read:
Cursor cursor = db.query("user", new String[]{"uname"}, null, null, null, null, null);
String cont = "";
while ( cursor.moveToNext() ){
String str = cursor.getString(cursor.getColumnIndex("uname"));
cont += str;
txt_content.setText(cont);
}
break;
}
}
}