创建一个sqlite类
public class Sqlite extends SQLiteOpenHelper { /** * @param context 上下文 * @param name 数据库的名字 * @param factory 工程游标 * @param version 版本号 */ public Sqlite(Context context) { super(context, "person.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //创建表 create table 创建 一个表为 person primary key 主键 autoincrement 自增长 db.execSQL("create table person(id integer primary key autoincrement," + "aid text," + "title text," + "description text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
创建一个操作数据的类
public class Dao { private SQLiteDatabase db; private final Sqlite sqlite; public Dao(Context context){ sqlite = new Sqlite(context); db = sqlite.getWritableDatabase(); } //添加 public void add(String aid , String title ,String description){ db.execSQL("insert into person(aid,title,description) values (?,?,?)",new Object[]{aid,title,description}); db.close(); } public List<User> selete(){ Cursor cursor = db.rawQuery("select * from person", null); List<User> list = new ArrayList<>(); while (cursor.moveToNext()){ String aid = cursor.getString(cursor.getColumnIndex("aid")); String title = cursor.getString(cursor.getColumnIndex("title")); String description = cursor.getString(cursor.getColumnIndex("description")); User u = new User(aid,title,description); list.add(u); } return list; } // // public void delete(String name){ // // db.execSQL("delete from person where name = ?", new Object[]{name} ); // // } //删除 public void delete(String aid){ db.execSQL("delete from person where aid = ?", new Object[]{aid} ); db.close(); } //修改 public void update(String aid, String title, String description,String tj){ db.execSQL("update person set aid = ? , title = ?, description = ? where aid = ?", new Object[]{aid,title,description,tj}); db.close(); } }然后进行使用 调用就行....