首先将您创建的数据库放入您的资产文件夹中。
将以下 Java 文件添加到您的项目中
package com.app.ourforms.database;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Vector;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBConnect extends SQLiteOpenHelper {
ArrayList<String> arraylistUrl;
public int GetCursor;
public DBConnect(Context context, String db_name) {
super(context, db_name, null, 2);
if (db != null && db.isOpen())
close();
this.myContext = context;
DB_NAME = db_name;
try {
createDataBase();
openDataBase();
} catch (IOException e) {
// System.out.println("Exception in creation of database : "+
// e.getMessage());
e.printStackTrace();
}
}
public void createDataBase() throws IOException {
// boolean dbExist = checkDataBase();
boolean dbExist = databaseExist();
if (dbExist) {
// System.out.println("Database Exist");
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDatabase() throws IOException {
InputStream input = myContext.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
input.close();
// System.out.println(DB_NAME + "Database Copied !");
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public boolean isOpen() {
if (db != null)
return db.isOpen();
return false;
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
public boolean databaseExist() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
// System.out.println("My Pathe is:- " + myPath);
// System.out.println("Open");
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
// System.out.println("checkDB value:" + checkDB);
// System.out.println("My Pathe is:- " + myPath);
} catch (Exception e) {
// database does't exist yet.
}
if (checkDB != null) {
// System.out.println("Closed");
checkDB.close();
// System.out.println("My db is:- " + checkDB.isOpen());
}
return checkDB != null ? true : false;
}
public Cursor execCursorQuery(String sql) {
Cursor cursor = null;
try {
cursor = db.rawQuery(sql, null);
GetCursor = cursor.getCount();
Log.i("Inside execCursorQuery try", sql);
} catch (Exception e) {
Log.i("Inside execCursorQuery exception", e.getMessage());
}
return cursor;
}
public void execNonQuery(String sql) {
try {
db.execSQL(sql);
// Log.d("SQL", sql);
} catch (Exception e) {
// Log.e("Err", e.getMessage());
} finally {
// closeDb();
}
}
// ****************** Declare all the global variable
// ****************************//
private Context myContext;
public String DB_PATH = "data/data/com.app.ourforms/databases/"; // path
// of
// your
// datbase
public static String DB_NAME = "OurForms.sqlite";// your database
// name
@SuppressWarnings("unused")
private static String ASSETS_DB_FOLDER = "db";
private SQLiteDatabase db;
public Vector<ArrayList<String>> getQuestion(String Query) {
Vector<ArrayList<String>> alldata = new Vector<ArrayList<String>>();
ArrayList<String> Question = new ArrayList<String>();
ArrayList<String> Option1 = new ArrayList<String>();
ArrayList<String> Option2 = new ArrayList<String>();
ArrayList<String> Option3 = new ArrayList<String>();
ArrayList<String> Option4 = new ArrayList<String>();
ArrayList<String> Option5 = new ArrayList<String>();
ArrayList<String> correct = new ArrayList<String>();
ArrayList<String> correctness = new ArrayList<String>();
ArrayList<String> Description = new ArrayList<String>();
Cursor alldata_ques = execCursorQuery(Query);
// GetCursor = alldata_ques.getCount();
if (alldata_ques != null) {
Log.e("Cursor length", "" + alldata_ques.getCount());
GetCursor = alldata_ques.getCount();
if (alldata_ques.getCount() > 0)
while (alldata_ques.moveToNext()) {
Question.add((alldata_ques.getString(alldata_ques
.getColumnIndex("Question"))));
Option1.add(alldata_ques.getString(alldata_ques
.getColumnIndex("A")));
Option2.add(alldata_ques.getString(alldata_ques
.getColumnIndex("B")));
Option3.add(alldata_ques.getString(alldata_ques
.getColumnIndex("C")));
Option4.add(alldata_ques.getString(alldata_ques
.getColumnIndex("D")));
Option5.add(alldata_ques.getString(alldata_ques
.getColumnIndex("E")));
correct.add(alldata_ques.getString(alldata_ques
.getColumnIndex("Answer")));
correctness.add(alldata_ques.getString(alldata_ques
.getColumnIndex("Correctness")));
Description.add(alldata_ques.getString(alldata_ques
.getColumnIndex("Description")));
}
alldata.add(Question);
alldata.add(Option1);
alldata.add(Option2);
alldata.add(Option3);
alldata.add(Option4);
alldata.add(Option5);
alldata.add(correct);
alldata.add(correctness);
alldata.add(Description);
// Log.d("VECTORSIZE", String.valueOf(alldata.size()));
}
return alldata;
}
public Vector<ArrayList<String>> getQuestions(String Query) {
Vector<ArrayList<String>> alldata = new Vector<ArrayList<String>>();
ArrayList<String> Question = new ArrayList<String>();
ArrayList<String> Option1 = new ArrayList<String>();
ArrayList<String> Option2 = new ArrayList<String>();
ArrayList<String> Option3 = new ArrayList<String>();
ArrayList<String> Option4 = new ArrayList<String>();
ArrayList<String> Option5 = new ArrayList<String>();
ArrayList<String> correct = new ArrayList<String>();
ArrayList<String> Description = new ArrayList<String>();
Cursor alldata_ques = execCursorQuery(Query);
if (alldata_ques != null) {
Log.e("Cursor length", "" + alldata_ques.getCount());
if (alldata_ques.getCount() > 0)
GetCursor = alldata_ques.getCount();
while (alldata_ques.moveToNext()) {
Question.add((alldata_ques.getString(alldata_ques
.getColumnIndex("Question"))));
Option1.add(alldata_ques.getString(alldata_ques
.getColumnIndex("A")));
Option2.add(alldata_ques.getString(alldata_ques
.getColumnIndex("B")));
Option3.add(alldata_ques.getString(alldata_ques
.getColumnIndex("C")));
Option4.add(alldata_ques.getString(alldata_ques
.getColumnIndex("D")));
Option5.add(alldata_ques.getString(alldata_ques
.getColumnIndex("E")));
correct.add(alldata_ques.getString(alldata_ques
.getColumnIndex("Answer")));
Description.add(alldata_ques.getString(alldata_ques
.getColumnIndex("Description")));
}
alldata.add(Question);
alldata.add(Option1);
alldata.add(Option2);
alldata.add(Option3);
alldata.add(Option4);
alldata.add(Option5);
alldata.add(correct);
alldata.add(Description);
// Log.d("VECTORSIZE", String.valueOf(alldata.size()));
}
return alldata;
}
}
在上面的代码中根据您的项目更改以下行:
public String DB_PATH = "data/data/YOUR_PACKAGE_NAME/databases/"; // path of your datbase
public static String DB_NAME = "OurForms.sqlite";// your database Name
在您的活动中编写以下代码第一次将您的数据库复制到应用程序。
// For Database Copy
myPrefs = getSharedPreferences("myPrefs", 0);
// sharedPreferences = PreferenceManager
// .getDefaultSharedPreferences(getApplicationContext());
Data.FIRST_TIME_LAUNCH = myPrefs.getBoolean("FIRST_TIME_LAUNCH", true);
if (Data.FIRST_TIME_LAUNCH) {
Log.i(TAG, "FIRST TIME*************************");
dbConnect = new DBConnect(getApplicationContext(),
"OurForms.sqlite");
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("FIRST_TIME_LAUNCH", false);
editor.commit();
}
希望它会帮助你。