Java 没有这样的表SQLite错误无法编译

Java 没有这样的表SQLite错误无法编译,java,android,sqlite,Java,Android,Sqlite,我有三个班,在那里流动 输入要搜索的条目:(来自MainActivity) 然后,SearchViewList类将根据intent中的搜索值显示列表 Intent i = getIntent(); input = i.getStringExtra("input"); String l = input; datasource = new DatabaseHelper(this); datasource.openDataBase(); List<Definition&

我有三个班,在那里流动

  • 输入要搜索的条目:(来自MainActivity)

  • 然后,SearchViewList类将根据intent中的搜索值显示列表

            Intent i = getIntent();
    
    input = i.getStringExtra("input");
    
    String l = input;
    
    datasource = new DatabaseHelper(this);
    datasource.openDataBase();
    
    List<Definition> values = datasource.getSearchedDefinition(l);
    // use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Definition> adapter = new ArrayAdapter<Definition>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
    
    ListView listView = getListView();
    listView.setTextFilterEnabled(true);
    
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            String str = ((TextView) view).getText().toString();
            Log.i(str, str + "");
    
            Intent i = new Intent(getApplicationContext(),
                    SearchedView.class);
            i.putExtra("value", str);
            startActivity(i);
    
        }
    });
    
    }

  • 下面是DatabaseHelper类中的getEntry()方法

    public String getEntry(String l) throws SQLException {
            Cursor c = myDataBase.rawQuery(
                    "SELECT entry FROM defintionstbl where entry = '"
                            + l + "'", null);
            if (c != null) {
                c.moveToFirst();
                if (c.getCount() <= 0) {
                    return null;
                }
                String entry = c.getString(0);
                return entry;
            }
    
            return null;
        }
    
    public List<Definition> getSearchedDefinition(String l) throws SQLException {
            List<Definition> entries = new ArrayList<Definition>();
            Cursor cursor = myDataBase.rawQuery(
                    "SELECT entry FROM definitionstbl where entry like '" + l + "%'", null);
    
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Definition entry = cursorToDefinition(cursor);
                entries.add(entry);
                cursor.moveToNext();
            }
            // make sure to close the cursor
            cursor.close();
    
            return entries;
        }
    
        private Definition cursorToDefinition(Cursor cursor) {
            Definition entry = new Definition();
            entry.setId(cursor.getLong(0));
            entry.setEntry(cursor.getString(0));
            return entry;
        }
    
    以下是从外部源复制数据库的代码:

    public class DatabaseHelper extends SQLiteOpenHelper {
    
        // The Android's default system path of your application database.
        private static String DB_PATH = "/data/data/com.gtxradeon.newversioncomputerdictionary/databases/";
    
        private static String DB_NAME = "dictionary.sqlite";
    
        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 DatabaseHelper(Context context) {
    
            super(context, DB_NAME, null, 1);
            this.myContext = context;
        }
    
        /**
         * Creates a empty database on the system and rewrites it with your own
         * database.
         * */
        public void createDataBase() throws IOException {
    
            boolean dbExist = checkDataBase();
    
            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.
                this.getReadableDatabase();
    
                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() {
    
            SQLiteDatabase checkDB = null;
    
            try {
                String myPath = DB_PATH + DB_NAME;
                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 + DB_NAME;
    
            // 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 + 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) {
    
        } 
    
    从MainActivity创建表

           try {
                String input = etSearch.getText().toString();
                Intent i = new Intent(this, SearchViewList.class);
                i.putExtra("input", input);
                Log.i("input", input + "");
                startActivity(i);  
    
            } catch (Exception e) {
                // TODO: handle exception
                String error = e.toString();
                Dialog d = new Dialog(this);
                d.setTitle("Row Empty or ID not found!");
                TextView tv = new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
                break;
    
            }
    
    try {             
    
                myDbHelper.createDataBase();
                myDbHelperTrivia.createDataBase();
                Log.i("CREATING", "DATABASE CREATED");
    
            } catch (Exception e) {
    
                Log.i("CREATE", "Exception Caught! ", e);
    
            }                           
    
            try {
    
                myDbHelper.openDataBase();
                Log.i("OPENING", "DATABASE OPENED");  
            } catch (SQLException e) {
    
                Log.i("OPEN", "Exception Caught! ", e);
    
            }
    

    谢谢你们的帮助,但我只是把getEntry方法改成了这个。。rawQuery给出了很多错误,但这种新方法很好用

    public String getEntry(String l) throws SQLException {
            String[] columns = new String[] { "_id",
                    "entry", "definition" };
            Cursor c = myDataBase.query("definitionstbl", columns,
                    "entry" + "='" + l + "'", null, null, null, null);
            if (c != null) {
                c.moveToFirst();
                if (c.getCount() <= 0) {
                    return null;
                }
                String entry = c.getString(1);
                return entry;
            }
    
            return null;
        }
    
    公共字符串getEntry(字符串l)引发SQLException{
    字符串[]列=新字符串[]{“\u id”,
    “条目”、“定义”};
    游标c=myDataBase.query(“definitionstbl”,列,
    “条目”+“=”“+l+””,null,null,null,null);
    如果(c!=null){
    c、 moveToFirst();
    
    如果(c.getCount(),你能上传创建表格的代码吗?现在已编辑。代码已包含在内。我看不到任何创建表格行。请尝试在你的android手机上使用此代码,并在获得错误@Kedarnath后搜索该表格(如果存在)。现在已包含该代码。抱歉
    try {             
    
                myDbHelper.createDataBase();
                myDbHelperTrivia.createDataBase();
                Log.i("CREATING", "DATABASE CREATED");
    
            } catch (Exception e) {
    
                Log.i("CREATE", "Exception Caught! ", e);
    
            }                           
    
            try {
    
                myDbHelper.openDataBase();
                Log.i("OPENING", "DATABASE OPENED");  
            } catch (SQLException e) {
    
                Log.i("OPEN", "Exception Caught! ", e);
    
            }
    
    public String getEntry(String l) throws SQLException {
            String[] columns = new String[] { "_id",
                    "entry", "definition" };
            Cursor c = myDataBase.query("definitionstbl", columns,
                    "entry" + "='" + l + "'", null, null, null, null);
            if (c != null) {
                c.moveToFirst();
                if (c.getCount() <= 0) {
                    return null;
                }
                String entry = c.getString(1);
                return entry;
            }
    
            return null;
        }