User Text in SQL Statement Cause App to Crash - android

I have an edittext, a button and a sqlite table. When the button is pressed a select statement runs that includes the text that was entered. The app works fine if the user only enters numbers, but crashes if it contains letters.
Here is the schema of the table:
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String STOP = "entryid";
public static final String LINE = "title";
public static final String SCHEDULE = "schedule";
}
The table is initialized. The onClick method is:
public void lookUp(View v) {
mEdit = (EditText) findViewById(R.id.text);
String string = mEdit.getText().toString();
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
FeedReaderContract.FeedEntry.STOP,
FeedReaderContract.FeedEntry.LINE,
FeedReaderContract.FeedEntry.SCHEDULE,
};
String sortOrder =
FeedReaderContract.FeedEntry.SCHEDULE + " ASC";
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME,
projection,
FeedReaderContract.FeedEntry.LINE+" = " + string ,
null,
null,
null,
sortOrder
);
Why does the app crashes when I have letters in the edittext? The columns are string types, so a string shouldn't crash it.
Here is the logcat error:
(1) no such column: line1
Shutting down VM
FATAL EXCEPTION: main
Process: com.mycompany.antibes, PID: 2767
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: android.database.sqlite.SQLiteException: no such column: line1 (code 1): , while compiling: SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC
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: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.mycompany.antibes.MainActivity.lookUp(MainActivity.java:61)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.view.View$1.onClick(View.java:4015) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Your sql is
SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC
which means it's comparing title to a column instead of a value.
Try adding single quotes around your string.
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME,
projection,
FeedReaderContract.FeedEntry.LINE+" = \'" + string + "\'" ,
null,
null,
null,
sortOrder
);
So you would get
SELECT entryid, title, schedule FROM entry WHERE title = 'line1' ORDER BY schedule ASC
which seems to be what you're trying to do.

Related

When i am trying to update data in SQLite database it showing an error :

I'm using SQLite for the first time, and trying to learn basic CRUD operation, so I'm facing problem when I am trying to update records. I searched about the solution, In solution they said uninstall the app and try, I tried but I got same error every time. Please help me to resolve this issue.
The Error I am getting
19 12:26:26.318 5113-5113/com.example.demointent E/SQLiteLog: (1) no such column: key_name
2019-08-19 12:26:26.320 5113-5113/com.example.demointent D/AndroidRuntime: Shutting down VM
2019-08-19 12:26:26.323 5113-5113/com.example.demointent E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demointent, PID: 5113
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: android.database.sqlite.SQLiteException: no such column: key_name (code 1 SQLITE_ERROR): , while compiling: UPDATE user_table SET key_name=?,key_phone=?,key_email=? WHERE id=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1667)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1615)
at com.example.demointent.MyDbHandler.updateUserDetails(MyDbHandler.java:96)
at com.example.demointent.UpdateUserActivity.update(UpdateUserActivity.java:46)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
This is My DBHelper class
public class MyDbHandler extends SQLiteOpenHelper {
private static String key_id = "id";
private static String key_name = "name";
private static String key_email = "email";
private static String key_phone = "phone";
private static String key_table = "user_table";
public MyDbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create_user = "CREATE TABLE "+key_table+"("+key_id+" INTEGER PRIMARY KEY ,"+key_name+" TEXT ,"+key_email+" TEXT ,"+key_phone+" TEXT "+")";
sqLiteDatabase.execSQL(create_user);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public boolean updateUserDetails(UserModel userModel){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("key_name",userModel.getName());
contentValues.put("key_email",userModel.getEmail());
contentValues.put("key_phone",userModel.getPhone());
database.update(key_table,contentValues,"id=?", new String[] {userModel.getId()});
return true;
}
You have declared
private static String key_name = "name";
Whereas you are trying to update user details by:
contentValues.put("key_name",userModel.getName());
The difference here is being "name" and "key_name". To solve this, you should be using column name constants for the CRUD operations.
contentValues.put(key_name,userModel.getName());
contentValues.put(key_email,userModel.getEmail());
contentValues.put(key_phone,userModel.getPhone());
Change this lines of code.
ContentValues contentValues = new ContentValues();
contentValues.put("key_name",userModel.getName());
contentValues.put("key_email",userModel.getEmail());
contentValues.put("key_phone",userModel.getPhone());
To
ContentValues contentValues = new ContentValues();
contentValues.put(key_name ,userModel.getName());
contentValues.put(key_email,userModel.getEmail());
contentValues.put(key_phone,userModel.getPhone());

Android: Getting string from cursor following a raw query in SQLITE

I am trying to get a string back from a raw query to my SQLITE database.
The issue is that I am bridging two tables, so I'm not sure how to call the column which contains the color (the value needed).
How can I get the string value being returned from the SQL query to my result variable?
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
String result = // get students.color from cursor;
return result
}
Thanks
EDIT
ERROR
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.me.echoboard, PID: 21552
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
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:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
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:6692) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.me.echoboard.DBHandler.studentColor(DBHandler.java:121)
at com.example.me.echoboard.login.onClick(login.java:37)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
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:6692) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
You are not moving within the cursor so it's at position -1 (before the first row)
Use the following this moves to the first row in the cursor (if there is one, if not an empty string will be returned) :-
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String result = "";
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
if (cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex("students.color"));
}
cursor.close();
return result
}
What you need to do is change your query with this and then cursor
"SELECT students.color as color FROM students, comments WHERE comments.author = students.name;";
Cursor
String result = cursor.getString(cursor.getColumnIndex("color"));

Can't get the data from a table: error while compiling

My app had to get some infos from the DataBase and display a table in a ListView.
So basicly, there is no Activity dedicated to the insert of the Data to the table.
And I'm doing it in the ModelHelper like that:
The user table contains: ID,Fname,Lname;
ID is PRIMARY KEY AUTOINCREMENT NOT NULL;
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
db.execSQL("INSERT INTO " + USER +" VALUES ('DOME','TRY')");
}
I did a method that returns the Fnames of the users
public ArrayList getOnlyNames(){
ArrayList lst = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT " +KEY_FNAME+ " from " +TABLE_QUESTION,null);
if (c.moveToFirst()) {
while(c.isAfterLast()==false){
String t1 = c.getString(1);
lst.add(t1);
c.moveToNext();
}
}
return lst;
}
In the Activity I declared the list:
ListView lst = (Listview) findViewById(R.id.list1);
and here's the used method:
public void showQuest(View view){
ArrayList<String> lstQ = md.getOnlyNames();
ArrayAdapter lstad = new ArrayAdapter(this, android.R.layout.simple_list_item_1,lstQ);
lst.setAdapter(lstad);
}
and I added Onlick"showquest" in a button in the same activity.
The log error is:
FATAL EXCEPTION: main
Process: com.example.root.myapplication, PID: 10225
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:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
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:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: android.database.sqlite.SQLiteException: no such column: Question (code 1): , while compiling: SELECT Question from Question
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
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:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
at model.sqlite.data.ModelHelper.getOnlyQuestions(ModelHelper.java:253)
at com.example.root.myapplication.kamiActivity.showQuest(kamiActivity.java:98)
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:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494)
The problem comes from the use of the method by the button, and I think the DB is empty, but I don't know why it can't take the column and it's already in the table.
You have requested data for only one column. The column index would be 0 not 1.
while(c.isAfterLast()==false){
String t1 = c.getString(0);
lst.add(t1);
c.moveToNext();
}
The exception says it all.
Caused by: android.database.sqlite.SQLiteException: no such column: Question (code 1): , while compiling: SELECT Question from Question
Safe method would be
c.getString(c.getColumnIndex(KEY_NAME));

No such column error in SQLITE database

I am getting error as NO SUCH COLUMN in logcat and it is inserting properly
How can I rectify this problem.Values which are given by me are representing correctly but SELECT query is not working for that values which are located in database table.
String search = "select * from "+ TABLE_NAME +" Where PHONENO =" +num+ "
and NAME = "+name1+" and LASTNAME =" +lastname1;
Cursor c = db.rawQuery(search,null);
boolean res = c.moveToFirst();
if (res) {
String s1 = c.getString(0);
String s2 = c.getString(1);
String s3 = c.getString(2);
String s4 = c.getString(3);
String s5 = c.getString(4);
String s6 = c.getString(5);
String s7 = c.getString(6);
String s8 = c.getString(7);
String s9 = c.getString(8);
String s10 = c.getString(9);
String s11 = c.getString(10);
String s12 = c.getString(11);
String s13 = c.getString(12);
String s14 = c.getString(13);
String s15 = c.getString(14);
String array[] ={s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15};
Intent i = new Intent(this,SelectMember.class);
i.putExtra("k1",array);
startActivity(i);
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.database, PID: 9112
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.database.sqlite.SQLiteException: no such column: durga (code 1): , while compiling: select * from MASTER_ACCOUNTS Where PHONENO =8121680624 and NAME = durga and LASTNAME =veerepalli 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.rawQuery(SQLiteDatabase.java:1255)
at com.example.user.database.Editoption.search(Editoption.java:36)
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:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.
String literals should be put in single quotes (')
Where PHONENO ='" +num+ "' and NAME = '"+name1+"' and LASTNAME ='" +lastname1 +"'";
By the way, this will blow up when the value contains a quote. Escape the string, or better use parameters.
Caused by: android.database.sqlite.SQLiteException: no such column:
durga (code 1): , while compiling: select * from MASTER_ACCOUNTS Where
PHONENO =8121680624 and NAME = durga and LASTNAME =veerepalli
Rectify Your Query first.
Informed you NO such table.
"SELECT * FROM "+ TABLE_NAME +" WHERE PHONENO ='" +num+ "' AND NAME = '"+ name1 +"' AND LASTNAME ='" + lastname1 +"'";
Then Uninstall old App and Clean-Rebuild-Run .

How Do I Create Tables Dynamically in SQLite in Android?

I am creating an application where a table gets generated in SQLite Database as soon as a new user registers, but I can create tables only in the onCreate() method in the SQLiteOpenHelper class.
I am using this user-defined function to create tables dynamically :
public void createUserTable(DatabaseOperations d,String user){
String USER_Q = "CREATE TABLE " + user + "(" + UserInfo.NOTES + " text);";
SQLiteDatabase dp = d.getWritableDatabase();
dp.execSQL(USER_Q);
}
but it does not work as I get the error while retreiving the notes column from the user table:
02-23 18:17:35.499 7292-7292/? E/SQLiteLog: (1) no such table: Bundle
02-23 18:17:35.500 7292-7292/? D/AndroidRuntime: Shutting down VM
02-23 18:17:35.507 7292-7292/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.himanshu.sqlregistration, PID: 7292
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.himanshu.sqlregistration/com.example.himanshu.sqlregistration.secretMessage}: android.database.sqlite.SQLiteException: no such table: Bundle (code 1): , while compiling: SELECT notes FROM Bundle[mParcelledData.dataSize=72]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.database.sqlite.SQLiteException: no such table: Bundle (code 1): , while compiling: SELECT notes FROM Bundle[mParcelledData.dataSize=72]
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: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.himanshu.sqlregistration.DatabaseOperations.extractNotesFromUser(DatabaseOperations.java:44)
at com.example.himanshu.sqlregistration.secretMessage.onCreate(secretMessage.java:24)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
at android.app.ActivityThread.access$800(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5343) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
Code for retreiving columns from table:
public Cursor extractNotesFromUser(DatabaseOperations d, String user) {
SQLiteDatabase dp = d.getReadableDatabase();
String column[] = {UserInfo.NOTES};
Cursor c = dp.query(user, column, null, null, null, null, null);
return c;
}
How can I create new tables with or without using the onCreate() method?
Error show that your table is not created, because you have put semicolon (;) at the end of query so write proper like below, its create runtime when you call this function.
public void createUserTable(DatabaseOperations d, String user) {
final SQLiteDatabase db = getWritableDatabase();
String CREATE_TABLE_NEW_USER = "CREATE TABLE " + user + " (" + UserInfo.NOTES + " TEXT)";
db.execSQL(CREATE_TABLE_NEW_USER);
db.close();
}

Categories

Resources