【android】 sqliter数据库CRUD
基础类,DBHelper
package com.duduli.li.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private final static String DB_NAME = "BOOK.DB"; public DBHelper(Context context) { super(context, DB_NAME, null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table bookinfo (_id integer PRIMARY KEY ,name varchar(20),position long,linemsg varchar(100),page int,fileplace varchar(40));"); } @Override public void onUpgrade(SQLiteDatabase db, int olderVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("drop table if exists bookinfo"); onCreate(db); } }
book类:
package com.duduli.li.db; public class Book { private int id; private String name; private Long position; private String line; private String filePlace; private int page; public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getFilePlace() { return filePlace; } public void setFilePlace(String filePlace) { this.filePlace = filePlace; } public String getLine() { return line; } public void setLine(String line) { this.line = line; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPosition() { return position; } public void setPosition(Long position) { this.position = position; } public int getId() { return id; } }
book的CRUD:
package com.duduli.li.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class BookDAO { private SQLiteDatabase db; private DBHelper dbh; public BookDAO(Context context){ dbh = new DBHelper(context); } @SuppressWarnings("unused") public void add(Book book){ db = dbh.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("name", book.getName()); cv.put("position", book.getPosition()); cv.put("linemsg", book.getLine()); cv.put("fileplace", book.getFilePlace()); cv.put("page", book.getPage()); db.insert("bookinfo", null, cv); db.close(); } public Cursor show(){ db = dbh.getReadableDatabase(); Cursor cur = db.query("bookinfo",null, null, null, null, null, null); System.out.println("show get the cursor count is "+cur.getCount()); db.close(); return cur; } public void delete(String s){ db = dbh.getWritableDatabase(); db.delete("bookinfo", "_id=?", new String[]{s}); db.close(); } public void update(int _id,long position,int page,String listMsg){ db = dbh.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("position", position); cv.put("page", page); cv.put("linemsg", listMsg); String [] arg = {String.valueOf(_id)}; db.update("bookinfo", cv, "_id=?", arg); db.close(); } }
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30