增:insert into 表名 (字段名,字段名) values (?,?,?)
删:delete from 表名 where 字段=?
改:update 表名 set 字段=?,字段=? where 条件=?
查:select * form 表名 where 字段=?
(*可以更改为想要查找的内容)
创建数据库
package com.bawei.zsgcactivity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySqlistHelper extends SQLiteOpenHelper{ public MySqlistHelper(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 db.execSQL("create table good (id integer primary key autoincrement,name char(50) not null, price char(50),producter char(50),color char(50) )"); db.execSQL("insert into good values(null,'小米','1000','北京','黄色')"); db.execSQL("insert into good values(null,'华为','1000','上海','红色')"); db.execSQL("insert into good values(null,'苹果','1000','深圳','白色')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
主方法调用
MySqlistHelper helper=new MySqlistHelper(this, "07a", null, 1);
SQLiteDatabase dp = helper.getReadableDatabase();
查询
Cursor c = dp.rawQuery("select * from good", null); while (c.moveToNext()) { int id = c.getInt(c.getColumnIndex("id")); String name=c.getString(c.getColumnIndex("name")); String price=c.getString(c.getColumnIndex("price")); String producter=c.getString(c.getColumnIndex("producter")); String color=c.getString(c.getColumnIndex("color")); list.add(new Goods(id, name, price, producter, color)); }
修改
MySqlistHelper helper=new MySqlistHelper(Xiougai.this, "07a", null, 1); dp = helper.getReadableDatabase(); dp.execSQL("update good set name=?,price=?,producter=?,color=? where id=?", new Object[]{name1,price1,producter1,color1,id});
增加
dp.execSQL("insert into good values(null,?,?,?,?)", new Object[]{name,price,producter,color});
删除
dp.execSQL("delete from good where id=?", new Object[]{list.get(position).getId()});