我遵循了本教程:http ://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
现在我按照我的 DatabaseHelper 类中看到的所有内容进行了操作:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import slo.bankomati.models.Atm;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* SQLite Database Interface
*/
public class DatabaseHelper extends SQLiteOpenHelper
{
// OPERATING VARS
private static String DB_PATH = "/data/data/slo.bankomati.core/databases/";
private static String DB_NAME = "nessus";
private SQLiteDatabase DATABASE;
private final Context CONTEXT;
// DATABASE TABLES
private static final String TABLE_ATM = "atms";
/**
* Stores a reference to Context class
* to allow access to application associated resources.
*/
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, 1);
this.CONTEXT = context;
}
/**
* DATABASE TABLE - CRUD METHODS
*/
public String[] getDBNames(){
String[] result = null;
try {
StringBuilder sb = new StringBuilder();
sb.append("SELECT name FROM " + DB_NAME);
sb.append("WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ");
sb.append("UNION ALL ");
sb.append("SELECT name FROM sqlite_temp_master ");
sb.append("WHERE type IN ('table','view') ");
sb.append("ORDER BY 1");
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sb.toString(), null);
c.moveToFirst();
result = new String[c.getCount()];
int i = 0;
while (c.moveToNext()) {
result[i]= c.getString(c.getColumnIndex("name"));
i++;
}
c.close();
}
catch(Exception e){
}
return result;
}
/**
* Returns all entries associated with
* table: Atms
*/
public List<Atm> getAllAtms()
{
// Reserve array for entries
List<Atm> all = new ArrayList<Atm>();
// Prepare SQL statement
String statement = "SELECT * FROM " + TABLE_ATM;
// Fetch db instance
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(statement, null);
int LIMIT_COUNTER = 0;
// Add all retrieved entries to array
if(cursor.moveToFirst())
{
do {
// Map table to object
Atm atm = new Atm(
cursor.getInt(0), // atm
cursor.getString(1), // owner
cursor.getString(2), // entity
cursor.getString(3), // address
cursor.getInt(4), // zip
cursor.getString(5), // city
cursor.getInt(6), // print
cursor.getInt(7), // deposit_unit
cursor.getInt(8), // gsm
cursor.getInt(9), // bills
cursor.getInt(10), // mastercard
cursor.getInt(11), // visa
cursor.getInt(12), // dubers
cursor.getInt(13), // transactions
cursor.getInt(14), // transfers
cursor.getInt(15), // deposit
cursor.getDouble(16), // latitude
cursor.getDouble(17) // longitude
);
// Store object
if(LIMIT_COUNTER < 5)
{
all.add(atm);
}
LIMIT_COUNTER++;
} while (cursor.moveToNext());
}
return all;
}
/**
* DATABASE - CREATE - UPDATE METHODS
*/
public synchronized void close()
{
if(DATABASE != null)
DATABASE.close();
super.close();
}
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
/**
* Create a empty database on and override it with
* applications own database.
*/
public void createDatabase()
{
if(!checkDatabase())
{
getReadableDatabase();
try {
// Atempt to copy over the database
copyDatabase();
} catch (Exception e) {
// Could not copy database
// TODO: Substitute Log for production version
Log.w("ILG", "Could not copy the database");
}
}
}
/**
* Checks if our database already exists on our system
* in order to avoid a copy over.
*
* @return boolean
*/
private boolean checkDatabase()
{
SQLiteDatabase test = null;
try {
// Attempt to open local database
test = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
// Exception caught database does not exsist
} finally {
// Close database
if(test != null)
test.close();
}
return (test != null) ? true : false;
}
/**
* Overrides newly created empty database
* with our own database.
*/
private void copyDatabase()
{
try {
// Open input file stream from our database
InputStream fis = CONTEXT.getAssets().open(DB_NAME);
// Open output file stream for our empty database
OutputStream fos = new FileOutputStream(DB_PATH + DB_NAME);
// Allocate 1024 bytes for file transfer per cycle
byte[] buffer = new byte[1024];
// Reference to the amount of byte read
int block;
// Read source until EOF
while( (block = fis.read(buffer)) > 0 )
fos.write(buffer, 0, block);
// Close FIS and FOS streams
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
// Could not override the database
// TODO: Replace with write production version
Log.w("ILG", "Could not write to destenation.");
}
}
/**
* Open source database.
*/
public void openDatabase()
{
try {
DATABASE = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception e) {
// TODO: Replace with write production version
Log.w("ILG", "Could not open database.");
}
}
}
问题是每当我运行它时,我都会收到一个 no such table 错误。之后我添加了 getDBNames 方法来检查我的数据库中实际存在哪些表名,但它只返回一个空 String[] 数组。