There is the main activation in which the code is executed by pressing a button:

package test.testdb; import android.database.Cursor; import android.database.SQLException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.EditText; import android.widget.TextView; import java.io.IOException; import test.testdb.Contract.Entry; import test.testdb.DBHeler; public class MainActivity extends AppCompatActivity { private TextView output; private EditText in; DBHeler db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new DBHeler(this); try { db.createDataBase(); db.openDataBase(); } catch (IOException ex) { } output = (TextView) findViewById(R.id.output); output.setMovementMethod(new ScrollingMovementMethod()); in = (EditText) findViewById(R.id.in); } @Override protected void onDestroy() { super.onDestroy(); db.close(); } public void search() { String string = in.getText().toString().trim(); String query = "SELECT " + Entry.COLUMN_COL2 + " FROM " + Entry.TABLE_NAME + " WHERE " + Entry.COLUMN_COL1 + " = " + "\"" + string + "\""; Cursor cursor = db.database.rawQuery(query, null); output.setText(""); if (!string.equals("")) { try { if (cursor.getCount() != 0) { while (cursor.moveToNext()) { int col2ColumnIndex = cursor.getColumnIndex(Entry.COLUMN_COL2); String col2 = cursor.getString(col2ColumnIndex); output.append(col2 + "\n\n"); } } } catch (SQLException ex) { } finally { cursor.close(); } } } public void onClickBtnSearch(View view) { search(); } } 

The DB is used ready, with a DB everything is all right. Here is the class code that uses this database.

 package test.testdb; import android.provider.BaseColumns; //Класс ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚. Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ для указания ΠΈΠΌΠ΅Π½ΠΈ Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ ΠΈ ΠΏΠΎΠ»Π΅ΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ public class Contract implements BaseColumns { private Contract() { }; public static final class Entry implements BaseColumns { public final static String TABLE_NAME = "table"; public final static String COLUMN_COL1 = "col1"; public final static String COLUMN_COL2 = "col2"; } } 

 package test.testdb; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; import android.util.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class DBHeler extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/test.testdb/databases/"; private static final String DATABASE_NAME = "datab.db"; private static final int DATABASE_VERSION = 1; public SQLiteDatabase database; private Context myContext; public DBHeler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; } 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 + DATABASE_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(DATABASE_NAME); String outFileName = DB_PATH + DATABASE_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; 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 + DATABASE_NAME; database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if(database != null) database.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } 

But when you press the button, it gives an error

 E/AndroidRuntime: FATAL EXCEPTION: main Process: test.testdb, PID: 2411 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: near "table": syntax error (code 1): , while compiling: SELECT col2 FROM table WHERE col1 = "ΠΌΠ°ΠΌΠ°" 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 test.testdb.MainActivity.search(MainActivity.java:52) at test.testdb.MainActivity.onClickBtnSearch(MainActivity.java:71) 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) 

Tell me where something is wrong? It seems everything is normal in the line where rawQuery

    1 answer 1

    It is written quite clearly:

    near "table": syntax error (code 1):, while compiling: SELECT col2 FROM table WHERE col1 = "mother"

    Not only the error message is given, but the error request itself. Don't you notice anything in it?

    The word TABLE is key in SQL. If a strange thought has already occurred to give the table such a name, then in the query you need to put back quotes:

     SELECT `col2` FROM `table` WHERE `col1` = "ΠΌΠ°ΠΌΠ°" 

    But it is better not to give such names to either the tables, or their fields, or anything else. Not in the sense of TABLE names, but names that coincide with keywords in principle.

    • And the register does not play a role? - post_zeew Nov.
    • @post_zeew, no effect. - PinkTux
    • Strange . - post_zeew Nov.
    • @post_zeew, and in which version of SQL / on which base does such a CREATE soar? Checked on postgres, sqlte3 and maria, never works. With back quotes, yes. - PinkTux
    • Yes, in theory, it should not be anywhere, but it was not a criticism, if that :) Just confused the answer to en.SO. - post_zeew Nov.