android - 由 : android. database.sqlite.SQLiteException : no such table while compiling: SELECT FROM. 引起。

标签 android mysql sqlite

我有一个问题,无法从我的数据库 sqlite 显示 ListView ,日志错误是: 引起原因:android.database.sqlite.SQLiteException:没有这样的表:tb_gejala(代码1):,编译时:SELECT nama_gejala。在我的浏览器 sqlite 中,此查询已成功运行。 我的代码有什么问题吗?

这是我的主要 Activity :

MyCustomAdapter dataAdapter = null;
DatabaseHelperInfopenyakit db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new DatabaseHelperInfopenyakit(this);
    setContentView(R.layout.main_deteksi);
    Button myButton = (Button) findViewById(R.id.button1);
    myButton.setOnClickListener(new OnClickListener() {

       // public AbsListView gejalaList;

        @Override
        public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                responseText.append("Gejala Yang dipilih adalah");

                ArrayList<Gejala> gejalaList = dataAdapter.gejalaList;
                for (int i = 0; i < gejalaList.size(); i++) {
                    Gejala gejala = gejalaList.get(i);
                    if (gejala.isSelected()) {
                        responseText.append("\n" + gejala.getName());
                    }
                }

                Toast.makeText(getApplicationContext(), responseText,
                        Toast.LENGTH_SHORT).show();

            }

    });

    displayListView();

}

    // Mendeklarasikan  arraylist gejalaList dan menginisialiasai dengan data dr db


        private ArrayList <Gejala> displayListView() {
            ArrayList<Gejala> gejalaList = new ArrayList<Gejala>();
            SQLiteDatabase sd = db.getReadableDatabase();
            Cursor cursor = sd.rawQuery("SELECT nama_gejala FROM tb_gejala", null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                gejalaList.add(new Gejala(cursor.getString(0)));
                cursor.moveToNext();
            }
            cursor.close();



            // Buata array adapter dari data gejalaList

            dataAdapter = new MyCustomAdapter(this, R.layout.gejala_row, gejalaList);

            ListView listView = (ListView) findViewById(R.id.listView1);
            // Assign adapter to ListView
            listView.setAdapter(dataAdapter);

            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // When clicked, show a toast with the TextView text
                    Gejala gejala = (Gejala) parent.getItemAtPosition(position);
                    Toast.makeText(getApplicationContext(),
                        "Clicked on Row: " + gejala.getName(),
                        Toast.LENGTH_SHORT).show();
                 }
            });
        return gejalaList;
        }



private class MyCustomAdapter extends ArrayAdapter<Gejala> {

    private ArrayList<Gejala> gejalaList;

    public MyCustomAdapter(Context context, int textViewResourceId,
                           List<Gejala> gejalaList) {
        super(context, textViewResourceId, gejalaList);
        this.gejalaList = new ArrayList<Gejala>();
        this.gejalaList.addAll(gejalaList);
    }



    private class ViewHolder {
        TextView id;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.gejala_row, null);

            holder = new ViewHolder();
          //  holder.id = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Gejala gejala = (Gejala) cb.getTag();
                    Toast.makeText(
                            getApplicationContext(),
                            "Clicked on Checkbox: " + cb.getText() + " is "
                                    + cb.isChecked(), Toast.LENGTH_SHORT)
                            .show();
                    gejala.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Gejala gejala = gejalaList.get(position);
       // holder.id.setText(" (id:" + gejala.getId() + ")");
        holder.name.setText(gejala.getName());
        holder.name.setChecked(gejala.isSelected());
        holder.name.setTag(gejala);
        return convertView;
    }
}

这是我的 DatabaseHelperInfoPenyakit:

private static String DB_PATH = "";

    private static String DB_NAME = "sistempakar.sqilte3";

    private SQLiteDatabase myDataBase;

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DatabaseHelperInfopenyakit(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
        DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
    }

    /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if(dbExist){
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            db_Read = this.getReadableDatabase();
            db_Read.close();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }

        }

    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){
     //  this.getReadableDatabase();

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH ;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH ;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH ;
        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();
      } 
    }

    }
    }

最佳答案

我想你可能已经解决了这个问题,但我想提供解决方案来帮助其他面临这个问题的人

问题:您最初可能为 DB_TABLE 使用了不同的名称,因此在安装更新时,android 不会删除包括数据库在内的应用程序数据。(在升级时,您只是复制现有数据库)

简单的解决方案:卸载现有应用程序并重新安装。

复杂的解决方案:在 DBManager 中添加逻辑以创建 DB_Table(如果它不存在)

关于android - 由 : android. database.sqlite.SQLiteException : no such table while compiling: SELECT FROM. 引起。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45261283/

相关文章:

android - Android 上的通用系统挂载

c# - 从 sqlite 数据库中选择数据并将值绑定(bind)到 Windows Phone 8 应用程序中的列表框项目

java - 将字符串的一部分设为可点击链接

java - 当 header 看起来正确时,为什么 Springframework 返回空主体?

MySQL 存储过程错误 1146

php - 如何将 Eloquent 'saveMany' 与数据透视表一起使用?

android - SQLDroid 与 Android 内部 JDBC 驱动程序

sql - 如何从子查询返回两个字段

java - Android 以编程方式创建的 ImageView 未显示

mysql - 在mysql中比较一个表和另一个表并显示匹配的记录