在尝试从SQLite db中获取值时,出现了以下错误: -
07-15 02:49:33.040: E/SQLiteLog(1770): (1) no such table: collegelist
07-15 02:49:33.040: D/AndroidRuntime(1770): Shutting down VM
07-15 02:49:33.050: W/dalvikvm(1770): threadid=1: thread exiting with uncaught exception (group=0xb2acaba8)
07-15 02:49:33.050: E/AndroidRuntime(1770): FATAL EXCEPTION: main
07-15 02:49:33.050: E/AndroidRuntime(1770): Process: com.existingsqlitedatabase, PID: 1770
07-15 02:49:33.050: E/AndroidRuntime(1770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.existingsqlitedatabase/com.existingsqlitedatabase.MainActivity}: android.database.sqlite.SQLiteException: no such table: collegelist (code 1): , while compiling: SELECT * FROM collegelist
07-15 02:49:33.050: E/AndroidRuntime(1770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-15 02:49:33.050: E/AndroidRuntime(1770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 02:49:33.050: E/AndroidRuntime(1770): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 02:49:33.050: E/AndroidRuntime(1770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 02:49:33.050: E/AndroidRuntime(1770): at android.os.Handler.dispatchMessage(Handler.java:102)
我已经检查了我的数据库(大学)中的表(同事)并且它在那里,但我不知道它为什么不读它。我还在这里查看了几篇与之相关的帖子,但是他们没有帮助。
这是我的DatabaseHelper类: -
public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="colleges";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseHelper(Context context)
{
super(context, DB_NAME, null, 1);// 1? its Database Version
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/**
* Getting all labels
* returns list of labels
* */
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM collegelist";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
}
请帮忙。
答案 0 :(得分:1)
从资产文件夹
加载数据库
public class DataHelper extends SQLiteOpenHelper {
public SQLiteDatabase database = null;
public File databaseFile;
public static String databaseName = "myDb";
public String databasePath = "";
Context mContext;
public DataHelper(Context paramContext) {
super(paramContext, databaseName, null, 1);
this.mContext = paramContext;
this.databasePath = ("data/data/" + paramContext.getPackageName()+"/databases/"+databaseName);
//this.databasePath = ("data/data/" + paramContext.getPackageName() + "/data.sqlite");
this.databaseFile = new File(this.databasePath);
if (!this.databaseFile.exists())
try {
deployDataBase(DataHelper.databaseName, this.databasePath);
return;
} catch (IOException localIOException) {
localIOException.printStackTrace();
}
}
private void deployDataBase(String dbNAme, String dbPath)
throws IOException {
InputStream localInputStream = this.mContext.getAssets().open(dbNAme);
FileOutputStream localFileOutputStream = new FileOutputStream(dbPath);
byte[] arrayOfByte = new byte[1024];
while (true) {
int i = localInputStream.read(arrayOfByte);
if (i <= 0) {
localFileOutputStream.flush();
localFileOutputStream.close();
localInputStream.close();
return;
}
localFileOutputStream.write(arrayOfByte, 0, i);
}
}
@Override
public synchronized void close() {
if (database != null)
database.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}