编译时不存在SQL异常

时间:2017-04-24 13:40:18

标签: java android mysql sqlite

我希望获得与用户获得的某些分数相对应的智商水平。我在资源文件夹中有我的数据库。数据库已成功创建并打开。但它显示NO SUCH TABLE ERROR,而我100%确定该表存在于我的score.db文件中。

public class DatabaseHelper extends SQLiteOpenHelper {

String DB_PATH = null;
private static String DB_NAME = "score";
private SQLiteDatabase myDataBase;
private final Context myContext;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, 10);
    this.myContext = context;
    this.DB_PATH = "/data/data/" + "com.example.madhav.loginandreg" + "/" + "databases/";
    Log.e("Path 1", DB_PATH);
}


public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[10];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}


@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();

        }
}

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
    return myDataBase.query(table, columns, null, null, null, null, null);

}

public String getiqscore(int rawscore,String condition,String age,String what){
    String query="Select  "+what +" from " +age+ " where " +condition+"="+rawscore+";";
    String data=null;
    myDataBase=this.getReadableDatabase();
    Cursor cursor=myDataBase.rawQuery(query,null);
    cursor.moveToFirst();
    if (cursor.getString(cursor.getColumnIndex(what)) != null)
        data = cursor.getString(cursor.getColumnIndex(what));

    cursor.close();
    myDataBase.close();
    return data;
}

}

上述指定代码的Java文件: -

public class information extends AppCompatActivity {
RadioGroup[] rp= new RadioGroup[11] ;
int score=0;
DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_information);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
          db=new DatabaseHelper(information.this);

    try {
        db.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        db.openDataBase();
    } catch (Exception sqle) {
        throw sqle;
    }

    rp[0]=(RadioGroup) findViewById(R.id.q1);
    rp[0]=(RadioGroup) findViewById(R.id.q1);
    rp[1]=(RadioGroup) findViewById(R.id.q2);
    rp[2]=(RadioGroup) findViewById(R.id.q3);
    rp[3]=(RadioGroup) findViewById(R.id.q4);
    rp[4]=(RadioGroup) findViewById(R.id.q5);
    rp[5]=(RadioGroup) findViewById(R.id.q6);
    rp[6]=(RadioGroup) findViewById(R.id.q7);
    rp[7]=(RadioGroup) findViewById(R.id.q8);
    rp[8]=(RadioGroup) findViewById(R.id.q9);
    rp[9]=(RadioGroup) findViewById(R.id.q10);
    rp[10]=(RadioGroup) findViewById(R.id.q11);

}
public void submit(View view){
    for(int i=0 ; i<11 ; i++){
    String  id= String.valueOf(rp[i].getCheckedRadioButtonId());
       int idi;
        try{
            idi=rp[i].getCheckedRadioButtonId();
            RadioButton r=(RadioButton) findViewById(idi);
            String a= (String) r.getText();
            if(a.equalsIgnoreCase("1           ")){
                score++;
            }
        }catch (Exception e){
            Toast.makeText(this,"Error",Toast.LENGTH_SHORT);
        }
    }

    Toast.makeText(this,String.valueOf(score),Toast.LENGTH_SHORT).show();
    int a=Integer.valueOf(score);
    String iq= db.getiqscore(a,"VinfoRs","age10","VinfoTq");
    Toast.makeText(this,iq,Toast.LENGTH_SHORT).show();
   // Intent intent=new Intent(this,activity3.class);
    //startActivity(intent);

}

}

enter code here java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                              at android.view.View.performClick(View.java:5610)
                                                                              at android.view.View$PerformClick.run(View.java:22265)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                              at android.view.View.performClick(View.java:5610) 
                                                                              at android.view.View$PerformClick.run(View.java:22265) 
                                                                              at android.os.Handler.handleCallback(Handler.java:751) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                                                                           Caused by: android.database.sqlite.SQLiteException: no such table: age10 (code 1): , while compiling: Select  VinfoTq from  age10 where VinfoRs=11;
                                                                              at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                              at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                              at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                              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:1318)
                                                                              at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
                                                                              at com.example.madhav.loginandreg.DatabaseHelper.getiqscore(DatabaseHelper.java:111)
                                                                              at com.example.madhav.loginandreg.information.submit(information.java:80)
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                              at android.view.View.performClick(View.java:5610) 
                                                                              at android.view.View$PerformClick.run(View.java:22265) 
                                                                              at android.os.Handler.handleCallback(Handler.java:751) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

2 个答案:

答案 0 :(得分:0)

删除byte中的静态值并使用InputStream中的实际值。

private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[myInput.available()];
        myInput.read(buffer);
        myOutput.write(buffer);
        myOutput.close();
        myInput.close();

    }

答案 1 :(得分:0)

您的数据库创建逻辑看起来很奇怪。我无法弄清楚DatabaseHelper正在扩展SQLiteOpenHelper的原因。您拨打SQLiteOpenHelper时唯一使用this.getReadableDatabase()功能的地方。但即便如此也是错误的。你不能使用它返回的SQLiteDatabase,并且(!!)你不能关闭它。

相反,您似乎将模板数据库文件复制到刚刚创建的只读数据库的顶部,然后打开它。我不知道会做什么,但实际上你最终可能会返回一个引用你的只读数据库的SQLiteDatabase ...这可能是空的。

无论哪种方式,这种逻辑似乎都被打破了。 (而且我不是Android程序员......)