作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试从 SQLite 数据库获取值时出现此错误:-
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;
}
}
请帮忙。
最佳答案
Load databaese from asset folder
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
}
}
关于java - SQLite异常: No such table even if there is table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751897/
我是一名优秀的程序员,十分优秀!