SQLite数据库的建立
public class MyHelper extends SQLiteOpenHelper{
public MyHelper(Context context){
super(context,"itcast.db",null,2);
}
public void onCreate(SQLiteDatabase db){
db.execSQl("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),price INTEGER)");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}
数据库的基本操作
public void insert(String name,String price){
MyHelper helper=new MyHelper(MainActivity.this);
SQliteDatabase db=helper.getWritableDababase();
ContentValues values =new ContentValues();
values.put("name",name);
values.put("price",price);
long id=db.insert("information",null,values);
db.close();
}
public int delete(long id){
SQliteDatabase db=helper.getWritableDatabase();
int number=db.delete("information","_id=?",new String[]{id+""});
db.close();
return number;
public int updata(String name,String price){
SQLiteDatavase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price",price);
int number=db.update("information",values,"name=?",new Strin[]{name});
db.close();
return number;
}
public void find(int id){
MyHelper helper=new MyHelper(MainActivity.this);
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.query("information",null,"_id=?",new String[]{id+""},null,null,null);
if(cursor.getCount()!=0){
while(cursor.moveToNext(){
String _id=cursor.getString(cursor.getColumnIndex("_id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
}
}
cursor.close();
db.close();
{
db.execSQL("insert into information(name,price) values(?,?)",new Object[]{name,price});
db.execSQL("delete from information where _id=1");
db.execSQL("updata information set name=? where price=?",new Object[]{name,price});
Cursor cursor=db.rawQuery("select * from information where name=?",new String[]{name});