1.首先制作扩展SQLiteOpenHelper的类
2.为该类创建一个承包商并实现所需的所有功能。
关于方法 onCreate((SQLiteDatabasedb)
只需创建名为查询的字符串并创建您想要的表,然后
execSql吧,例如:
String query="如果不存在表创建表(文本文本,名称
text2,_id text)"; db.execSQL(query); 然后在
sql lite 使用:
public void add_new_row(String text,String text2,String _id){ try{
ContentValues cn=new ContentValues(); cn.put("text",per_name);
cn.put("text2",per_age); cn.put("_id",per_age);
SQLiteDatabase db; db=getWritableDatabase();
db.insert("table",null,cn);} catch(Exception e){
Log.i("AddSql_lite","Print :"+e.to );
} And for read Row from sqllite you just use :
public String getRow_by_id(String _id){ string text = ""; try{
db=getWritableDatabase(); query="select * from table where _id =
'"+_id+"'";
Cursor cr=db.rawQuery(query, null);
if(cr.moveToFirst()){
do{
text = cr.getString(0);
String text2 = cr.getString(1);
String _id = cr.getString(2);
}while(cr.moveToNext());
}
}catch(Exception e){
}
return text; } And for reading all the rows in the table one after another :
public int get_all_Rows(){ string int counter = 0; try{
db=getWritableDatabase(); query="select * from table";
Cursor cr=db.rawQuery(query, null);
if(cr.moveToFirst()){
do{
text = cr.getString(0);
String text2 = cr.getString(1);
String _id = cr.getString(2);
counter++;
}while(cr.moveToNext());
}
}catch(Exception e){
}
return counter; } Hope its will help you to understand how Sqllite work and how build it correctly :)
为了让它运行得这么简单:
SQLiteDataBase Sqllite_ref = new SQLiteDataBase(context, "VerName",
null, 1);
Sqllite_ref.add_new_row("hey","hey4","1");
Sqllite_ref.add_new_row("hey2","hey5","2");
Sqllite_ref.add_new_row("hey3","hey6","3");
Sqllite_ref.getRow_by_id("2");>>> will return "hey2"
Sqllite_ref.get_all_Rows()>>> will return 3 [number of items you have
in the sqllite]