Android开发(SQULITE错误):编译SELECT * FROM时没有这样的表“”

时间:2017-06-30 19:05:15

标签: android sqlite

这是我在资源文件夹中的sqlite文件,它是从sql管理器导入的

    DROP TABLE IF EXISTS "Places";
    CREATE TABLE "Places" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT , "Place1" TEXT, "Place2" TEXT);
    INSERT INTO "Places" VALUES(1,'ABC','DEF');
    INSERT INTO "Places" VALUES(2,'XYZ','MNO');
    DROP TABLE IF EXISTS "android_metadata";
    CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
    INSERT INTO "android_metadata" VALUES('en_US');

这是我的dbopenhelper类

   public class OpenHelper extends SQLiteOpenHelper {

private static final String db_name="Places.db";
public Context context;
private static String db_path;
private SQLiteDatabase sqLiteDatabase;
@Override
public void onCreate(SQLiteDatabase db) {
    context.openOrCreateDatabase(db_name,context.MODE_PRIVATE,null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public OpenHelper(Context context){
    super(context,db_name,null,1);
    db_path=context.getApplicationInfo().dataDir+"/databases/";
    context.openOrCreateDatabase(db_name,context.MODE_PRIVATE,null);
    this.context=context;
}
public void create_db() {
    boolean db_exist = checkdb();
    if (!db_exist) {
        this.getReadableDatabase();
        this.close();


        try {
            copyDatabase();
        } catch (IOException io) {
            throw new Error("Error Copying Database");
        }
    }
}
private void copyDatabase() throws IOException {
    InputStream m_in=context.getAssets().open(db_name);
    String opfile=db_path+db_name;
    OutputStream m_out=new FileOutputStream(opfile);
    byte[] buffer=new byte[1024];
    int len=m_in.read(buffer);
    while(len>0){
        m_out.write(buffer,0,len);
    }
    m_in.close();
    m_out.close();

}

private boolean checkdb() {
    File db_file=new File(db_path+db_name);
    return(db_file.exists());
}
public SQLiteDatabase openDatabase() throws SQLException {
    String path = db_path + db_name;
    if (sqLiteDatabase == null) {
        create_db();
        sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }
    return sqLiteDatabase;
}

public void check1(){
    Cursor c=sqLiteDatabase.query("Places", null, null, null, null,null, null);
    if(c.moveToFirst())
    {
        do {

            Toast.makeText(context,
                    "1 " + c.getString(0) + "\n"+
                    "2 " + c.getString(1) ,
                    Toast.LENGTH_LONG).show();

        } while (c.moveToNext());
    }
}
 @Override
    public synchronized void close(){
        if(database!=null){
            database.close();
        }
        super.close();
    }
}

我正在调用opendatabase并检查此类:

public class loc_edit extends Activity {
    private OpenHelper dbOpenHelper;
    public Context context;



@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loc_edit);



    dbOpenHelper = new OpenHelper(loc_edit.this);
    dbOpenHelper.openDatabase();
    dbOpenHelper.check1();



}
public void onDestroy()
{
    super.onDestroy();
    dbOpenHelper.close();
}

}

这是LOGCAT

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shashankbsharma.ksndmc/com.example.shashankbsharma.ksndmc.loc_edit}: android.database.sqlite.SQLiteException: no such table: Places (code 1): , while compiling: SELECT * FROM Places
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
    at android.app.ActivityThread.access$1000(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:5527)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: android.database.sqlite.SQLiteException: no such table: Places (code 1): , while compiling: SELECT * FROM Places
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
    at com.example.shashankbsharma.ksndmc.OpenHelper.check1(OpenHelper.java:84)
    at com.example.shashankbsharma.ksndmc.loc_edit.onCreate(loc_edit.java:29)
    at android.app.Activity.performCreate(Activity.java:6303)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)
    ... 9 more

我无法访问Places表,但我可以访问android_metadata表。 关于这个问题有很多线索,但没有一个是富有成效的。 任何帮助都将得到真正的赞赏。

0 个答案:

没有答案