Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as Java by tuan ( 7 years ago )
package com.example.vuhuu.tuanprj.ultil;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import com.example.vuhuu.tuanprj.activity.Activity_Main;
import com.example.vuhuu.tuanprj.model.Book;
import com.example.vuhuu.tuanprj.model.Category;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
/**
* Created by vuhuu on 11/7/2017.
*/
public class HowkSQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "tuanprj.sqlite";
private static final String DATABASE_PATH = "data/data/com.example.vuhuu.tuanprj/databases/";
private static final int DATABASE_VERSION = 1;
private final String TAG = Activity_Main.TAG;
private Context mContext;
private SQLiteDatabase mDatabase;
public HowkSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.mContext = context;
}
public void OpenDatabase(){
String dbPath = mContext.getDatabasePath(DATABASE_NAME).getPath();
if(mDatabase != null && mDatabase.isOpen()){
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public void CloseDatabase(){
if (mDatabase != null){
mDatabase.close();
}
}
public Cursor GetData(String sql){
mDatabase = getWritableDatabase();
Cursor c = mDatabase.rawQuery(sql,null);
return c;
}
public void QueryData(String sql){
mDatabase = getWritableDatabase();
mDatabase.execSQL(sql);
}
public boolean copyDatabase(){
try{
InputStream inputStream = mContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[] buff = new byte[1024];
int length = 0;
while ((length = inputStream.read(buff)) > 0){
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w(TAG,"DB Copied");
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public void CheckDatabase(){
File database = mContext.getDatabasePath(DATABASE_NAME);
if(!database.exists()){
this.getReadableDatabase();
if(copyDatabase()){
Toast.makeText(mContext,"Copy succsess",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(mContext,"Copy fail",Toast.LENGTH_SHORT).show();
return;
}
}
}
public ArrayList<Category> getListCategory(){
Category category = null;
ArrayList<Category> categoryArrayList = new ArrayList<>();
OpenDatabase();
Cursor cursor = GetData("SELECT * FROM category");
cursor.moveToFirst();
while (!cursor.isAfterLast()){
category = new Category(cursor.getInt(0),cursor.getString(1));
categoryArrayList.add(category);
cursor.moveToNext();
}
cursor.close();
CloseDatabase();
return categoryArrayList;
}
public ArrayList<Book> getListBook(int categoryid){
Log.d(TAG,"categoryid:"+categoryid);
Book book = null;
ArrayList<Book> bookArrayList = new ArrayList<>();
OpenDatabase();
Cursor cursor = null;
try {
if (categoryid == 0) {
cursor = GetData("SELECT * FROM books");
} else {
cursor = GetData("SELECT * FROM books WHERE categoryid = " + categoryid);
}
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
book = new Book(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),
//cursor.getBlob(4),
cursor.getString(5), cursor.getInt(6), cursor.getInt(7), cursor.getInt(8), cursor.getString(9), cursor.getInt(10),
cursor.getString(11), cursor.getString(12));
bookArrayList.add(book);
cursor.moveToNext();
}
}finally {
cursor.close();
}
CloseDatabase();
return bookArrayList;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Revise this Paste