package com.lgs.mvpmodule.bookstore.presenter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.concurrent.locks.ReentrantLock;
/**
* 保存书籍信息表
*/ public class NovelTbl extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "books.db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "novels";
public final static String ID = "id";//0 id 数据库中加入此字段
public final static String FILE_TYPE = "file_type";//1 资源名称
public final static String FILE_NAME = "file_name";//2 中文名称
public final static String AUTHOR = "author";// 3:作者
public final static String TITLE = "title";//4:标题
public final static String DESCRIBE = "describe";//5:描述
public NovelTbl(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// 创建table
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID
+ " INTEGER PRIMARY KEY NOT NULL UNIQUE DEFAULT ( 0 ), " + FILE_TYPE + " INT DEFAULT ( 1 ), "
+ FILE_NAME + " TEXT DEFAULT ( '' ), " + TITLE
+ " TEXT DEFAULT ( '' ), " + AUTHOR
+ " TEXT DEFAULT ( '' ), " + DESCRIBE + " TEXT DEFAULT ( '' ));";
db.execSQL(sql);
// CREATE TABLE poems (
// id INTEGER PRIMARY KEY AUTOINCREMENT
// NOT NULL,
// file_type INT DEFAULT (1),
// file_name TEXT (200) NOT NULL,
// title TEXT (200) NOT NULL,
// author TEXT (50) DEFAULT lgs,
// describe TEXT
// );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//重建数据库,所有数据丢失
if (oldVersion != newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
public Cursor select() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
// 增加操作
public long insert(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
/* ContentValues */
ContentValues cv = new ContentValues();
cv.put(ID, id.toString());
long row = db.insert(TABLE_NAME, null, cv);
return row;
// INSERT INTO book_info ( id ) VALUES ( 0 );
}
public long insert(String sInfo) {
String[] aryInfo = sInfo.split(",");
// String name = aryInfo[0];
if (aryInfo.length < 5)
return -1;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ID, Integer.valueOf(aryInfo[0]));
cv.put(FILE_TYPE, String.valueOf(aryInfo[1]));
cv.put(FILE_NAME, String.valueOf(aryInfo[2]));
cv.put(TITLE, String.valueOf(aryInfo[3]));
cv.put(AUTHOR, String.valueOf(aryInfo[4]));
cv.put(DESCRIBE, String.valueOf(aryInfo[5]));
long row = db.insert(TABLE_NAME, null, cv);
return row;
//INSERT INTO novels ( id,file_type,file_name,title,author,describe )
// VALUES (21722,2,"sanguo","古典名著:三国演义","元末明初:罗贯中","");
}
// 删除操作
public void delete(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
String where = ID + " = ?";
String[] whereValue = {id.toString()};
db.delete(TABLE_NAME, where, whereValue);
// delete from book_info;
}
// 修改操作
public long update(Integer id, String sInfo) {
String[] aryInfo = sInfo.split(",");
// String name = aryInfo[0];
if (aryInfo.length < 4)
return -1;
SQLiteDatabase db = this.getWritableDatabase();
String where = ID + " = ?";
String[] whereValue = {id.toString()};
ContentValues cv = new ContentValues();
cv.put(ID, Integer.valueOf(aryInfo[0]));
cv.put(FILE_TYPE, String.valueOf(aryInfo[1]));
cv.put(FILE_NAME, String.valueOf(aryInfo[2]));
cv.put(TITLE, String.valueOf(aryInfo[3]));
cv.put(AUTHOR, String.valueOf(aryInfo[4]));
cv.put(DESCRIBE, String.valueOf(aryInfo[5]));
long row = db.update(TABLE_NAME, cv, where, whereValue);
return row;
// UPDATE novels SET id = 2131230722,file_type=2, file_name = "sanguo" , title = "古典名著:三国演义" , author = "元末明初:罗贯中" , describe = "" WHERE id = 0
}
private final ReentrantLock mLock = new ReentrantLock(true);
public void lock() {
mLock.lock();// 多线程安全
}
public void unlock() {
mLock.unlock();
}
}
|