android sqlite

android

2013-07-03 16:14

   //打开或创建test.db数据库  
        SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);  
        db.execSQL("DROP TABLE IF EXISTS person");  
        //创建person表  
       db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");  
        Person person = new Person();  
        person.name = "john";  
        person.age = 30;  
        //插入数据  
        db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[]{person.name, person.age});  
        person.name = "david";  
        person.age = 33;  
        //ContentValues以键值对的形式存放数据  
        ContentValues cv = new ContentValues();  
        cv.put("name", person.name);  
        cv.put("age", person.age);  
        //插入ContentValues中的数据  
        db.insert("person", null, cv);  
          
        cv = new ContentValues();  
        cv.put("age", 35);  
        //更新数据  
        db.update("person", cv, "name = ?", new String[]{"john"});  
          
        Cursor c = db.rawQuery("SELECT * FROM person WHERE age >= ?", new String[]{"33"});  
        while (c.moveToNext()) {  
            int _id = c.getInt(c.getColumnIndex("_id"));  
            String name = c.getString(c.getColumnIndex("name"));  
            int age = c.getInt(c.getColumnIndex("age"));  
            Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);  
        }
       c.close();  
          
        //删除数据  
       db.delete("person", "age < ?", new String[]{"35"});  
          
        //关闭当前数据库  
        db.close();  
          
        //删除test.db数据库  
//      deleteDatabase("test.db"); 





public class Person {
    
    
    public String name;
    public int age;
    
    public Person(){
        
        
    }
    

}


--------------------------SQLiteOpenHelper ------------------------------------------------------------------


public class mydb extends SQLiteOpenHelper {

final String table_name = "new_table";



public mydb(Context context, String name, CursorFactory factory, int version) {

super(context, name, factory, version);

// TODO Auto-generated constructor stub

}


@Override

public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub


String sql=" CREATE TABLE ["+table_name+"] (";

sql+="[_id] INTEGER  PRIMARY KEY AUTOINCREMENT,";

sql+="[title] VARCHAR(30),";

sql+="[content] VARCHAR(80) )";

Log.d("DB", sql);

db.execSQL(sql);

}


@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub


}


}


mdb= new mydb(this, "e2e.db", null, 1);

db=mdb.getWritableDatabase();

cv = new ContentValues();


cv.put("title", "哇咔咔");

cv.put("content", "哇咔咔222");

db.insert(mdb.table_name, null, cv);



cv.put("title", "哇咔咔");

cv.put("content", "哇咔咔3333");

db.update(mdb.table_name, cv, null, null);



Cursor c=db.query(mdb.table_name, null, null, null, null, null, null);

c.moveToFirst();

while(c.moveToNext()){

int id=c.getInt(c.getColumnIndex("_id"));

String tiele=c.getString(c.getColumnIndex("title"));

String content=c.getString(c.getColumnIndex("content"));

Log.d("DB","id:"+id+" title:"+tiele+"content"+content );

}