Package com. jk. service;/*** create and update the database version */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 {public DbHelper (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version);} p Ublic DbHelper (Context context) {super (context, "diary. db ", null, 1) ;}@ Overridepublic void onCreate (SQLiteDatabase db) {String SQL =" CREATE TABLE Diary "+" (diaryId integer primary key autoincrement, title varchar (20), body varchar (1000), time varchar (40) "; db.exe cSQL (SQL) ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// String SQL = "ALTER TABLE diary ADD body var Char (1000) null "; // db.exe cSQL (SQL); System. out. println (" DataBase Updated !! ");}}
Package com. jk. service; import com. jk. diary; import java. util. arrayList; import java. util. list; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase;/*** add, delete, modify, and query the database * @ author jkxqj */public class DiaryAdapter {private DbHelper dbOpenHelper; public DiaryAdapter (Context context) {dbOpenHelper = new DbHelper (context);} public void save (Diary diar Y) {/* The getWritableDatabase and getReadableDatabase () methods are the same when the disk space is not full. The getWritableDatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write data. If getWritableDatabase () is used, an error occurs. The getReadableDatabase () method first opens the database in read/write mode. If the disk space of the database is full, the opening fails. When the opening fails, the database will continue to be opened in read-only mode. If the problem is solved successfully, the read-only database object is closed and a read/write database object is returned. */SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();/* SQLiteDatabase db2 = dbOpenHelper. getWritableDatabase (); * // db = db2String SQL = "insert into Diary (title, body, time) values (?,?,?) "; // Prevent potential bugdb.exe cSQL (SQL, new Object [] {diary. getTitle (), diary. getBody (), diary. getTime ()}); db. close ();} public void update (Diary diary) {SQLiteDatabase db = dbOpenHelper. getWritableDatabase (); String SQL = "update Diary set title = ?, Body =? Where diaryId =? "; Db.exe cSQL (SQL, new Object [] {diary. getTitle (), diary. getBody (), diary. getId ()}); db. close ();} public void delete (Integer id) {SQLiteDatabase db = dbOpenHelper. getWritableDatabase (); String SQL = "delete from Diary where diaryId =? "; Db.exe cSQL (SQL, new Object [] {id}); db. close ();} public Diary find (Integer id) {SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); String SQL = "select * from Diary where diaryId =? "; Cursor cursor = db. rawQuery (SQL, new String [] {id. toString ()}); if (cursor. moveToFirst () {// move the cursor to the first record. If the cursor is successfully transferred, int diaryId = cursor is recorded. getInt (cursor. getColumnIndex ("diaryId"); String title = cursor. getString (cursor. getColumnIndex ("title"); String body = cursor. getString (cursor. getColumnIndex ("body"); String time = cursor. getString (cursor. getColumnIndex ("time"); return new Diary (diaryId, title, Body, time) ;}return null;} public List <Diary> getScrollData (int offset, int maxResult) {List <Diary> Diarys = new ArrayList <Diary> (); SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); // offset indicates the offset. maxResult indicates the maximum result set String SQL = "select * from Diary limit ?,? "; Cursor cursor = db. rawQuery (SQL, new String [] {String. valueOf (offset), String. valueOf (maxResult)}); while (cursor. moveToNext () {// If the cursor is moved to the next part, int diaryId = cursor is not read. getInt (cursor. getColumnIndex ("diaryId"); String title = cursor. getString (cursor. getColumnIndex ("title"); String body = cursor. getString (cursor. getColumnIndex ("body"); String time = cursor. getString (cursor. getColumnIndex ("time"); Dia Rys. add (new Diary (diaryId, title, body, time);} db. close (); cursor. close (); return Diarys;} public Cursor getCursorScrollData (int offset, int maxResult) {SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); // offset indicates the offset. maxResult indicates the maximum result set String SQL = "select diaryId as _ id, title, body, time from Diary limit ?,? "; // SimpleCursorAdapter requires the _ id field Cursor cursor = db. rawQuery (SQL, new String [] {String. valueOf (offset), String. valueOf (maxResult)}); return cursor;} public long getCount () {SQLiteDatabase db = dbOpenHelper. getWritableDatabase (); String SQL = "select count (*) from Diary"; Cursor cursor = db. rawQuery (SQL, null); cursor. moveToFirst (); db. close (); long I = cursor. getLong (0); cursor. close (); return I ;}}