SQLiteException:即使有表没有这样的表 [英] SQLiteException: No such table even if there is table
本文介绍了SQLiteException:即使有表没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的当试图从SQLite的读取值20dB此错误传来: - 的
二月七日至15日:49:33.040:E / SQLiteLog(1770):(1)没有这样的表:collegelist
2月7日至15日:49:33.040:D / AndroidRuntime(1770):关闭VM
2月7日至15日:49:33.050:W / dalvikvm(1770):主题ID = 1:螺纹未捕获的异常(组= 0xb2acaba8)退出
2月7日至15日:49:33.050:E / AndroidRuntime(1770):致命异常:主要
2月7日至15日:49:33.050:E / AndroidRuntime(1770):工艺:com.existingsqlitedatabase,PID:1770
2月7日至15日:49:33.050:E / AndroidRuntime(1770):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.existingsqlitedatabase / com.existingsqlitedatabase.MainActivity}:android.database.sqlite.SQLiteException:没有这样的表:collegelist(code 1),在编译:SELECT * FROM collegelist
2月7日至15日:49:33.050:E / AndroidRuntime(1770):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
2月7日至15日:49:33.050:E / AndroidRuntime(1770):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
2月7日至15日:49:33.050:E / AndroidRuntime(1770):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
2月7日至15日:49:33.050:E / AndroidRuntime(1770):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
2月7日至15日:49:33.050:E / AndroidRuntime(1770):在android.os.Handler.dispatchMessage(Handler.java:102)
的我在我的数据库(学院)检查表(collegelist),它是有,但我不知道为什么它不读它。我也曾在这里检查一些与此相关的帖子较早,但他们并没有帮助。的
这是我的DatabaseHelper类: -
公共类DataBaseHelper扩展SQLiteOpenHelper
{
私有静态字符串标记=DataBaseHelper; //标签只是为了LogCat中窗口
//我们的目标设备上的数据库路径(位置)
私人静态字符串DB_PATH =;
私人静态字符串DB_NAME =学院; //数据库名称
私人SQLiteDatabase mDataBase;
私人最终上下文mContext;公共DataBaseHelper(上下文的背景下)
{
超(背景下,DB_NAME,NULL,1); // 1?它的数据库版本
DB_PATH = context.getApplicationInfo()DATADIR +/数据库/。
this.mContext =背景;
}公共无效的CreateDatabase()抛出IOException异常
{
//如果数据库不存在从资产复制布尔mDataBaseExist = checkDataBase();
如果(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
尝试
{
//复制从资产产生数据库
copyDataBase();
Log.e(TAG的CreateDatabase数据库中创建);
}
赶上(IOException异常mIOException)
{
抛出新的错误(ErrorCopyingDataBase);
}
}
}
//检查数据库存在这里:/数据/数据/软件包/数据库/ DA名称
私人布尔checkDataBase()
{
文件DBFILE =新的文件(DB_PATH + DB_NAME);
//Log.v(\"dbFile,DBFILE ++ dbFile.exists());
返回dbFile.exists();
}//复制从资产数据库
私人无效copyDataBase()抛出IOException异常
{
InputStream的mInput = mContext.getAssets()开(DB_NAME)。
字符串outFileName = DB_PATH + DB_NAME;
的OutputStream mOutput =新的FileOutputStream(outFileName);
字节[] = mBuffer新的字节[1024];
INT mLength;
而((mLength = mInput.read(mBuffer))大于0)
{
mOutput.write(mBuffer,0,mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}//打开数据库,所以我们可以查询它
公共布尔的openDatabase()抛出的SQLException
{
字符串的mpath = DB_PATH + DB_NAME;
//Log.v(\"mPath的mpath);
mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.CREATE_IF_NECESSARY);
// mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
返回mDataBase!= NULL;
}@覆盖
公共同步无效的close()
{
如果(mDataBase!= NULL)
mDataBase.close();
super.close();
}@覆盖
公共无效的onCreate(SQLiteDatabase为arg0){
// TODO自动生成方法存根}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根} / **
*让所有标签
*返回标签列表
* * /
公开名单<串GT; getAllLabels(){
清单<串GT;标签=新的ArrayList<串GT;(); //选择所有查询
字符串selectQuery =SELECT * FROM collegelist SQLiteDatabase分贝= this.getReadableDatabase();
光标光标= db.rawQuery(selectQuery,NULL); //通过所有行循环,并增加列表
如果(cursor.moveToFirst()){
做{
labels.add(cursor.getString(1));
}而(cursor.moveToNext());
} //关闭连接
cursor.close();
db.close(); //返回标贴
返回标签;
}
}
请帮忙。
解决方案
从资源文件夹加载databaese
块引用>公共类DataHelper扩展SQLiteOpenHelper {公共SQLiteDatabase数据库= NULL;
公共文件databaseFile;
公共静态字符串的databaseName =MYDB;
公共字符串databasePath =;
上下文mContext;公共DataHelper(上下文paramContext){ 超(paramContext,的databaseName,空,1);
this.mContext = paramContext;
this.databasePath =(数据/数据/+ paramContext.getPackageName()+/数据库/+的databaseName);
//this.databasePath =(数据/数据/+ paramContext.getPackageName()+/data.sqlite);
this.databaseFile =新的文件(this.databasePath);
如果(!this.databaseFile.exists())
尝试{
deployDataBase(DataHelper.databaseName,this.databasePath);
返回;
}赶上(IOException异常localIOException){
localIOException.printStackTrace();
}
}私人无效deployDataBase(DBNAME字符串,字符串DBPATH)
抛出IOException
InputStream的localInputStream = this.mContext.getAssets()开(数据库)。
FileOutputStream中localFileOutputStream =新的FileOutputStream(DBPATH);
字节[] = arrayOfByte新的字节[1024];
而(真){
INT I = localInputStream.read(arrayOfByte);
如果(ⅰ&下; = 0){
localFileOutputStream.flush();
localFileOutputStream.close();
localInputStream.close();
返回;
}
localFileOutputStream.write(arrayOfByte,0,I);
}
}@覆盖
公共同步无效的close(){ 如果(数据库!= NULL)
database.close(); super.close();}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根}}While trying to fetch the values from SQLite db this error came:-
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)
I have checked the table(collegelist) in my database(colleges) and it is there but I don't know why it's not reading it. I have also checked here few earlier posts related to it but they didn't helped.
This is my DatabaseHelper class:-
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; } }
Please help.
解决方案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 } }
这篇关于SQLiteException:即使有表没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文