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 .
Related
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"));
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));
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();
}
This line of code puts my three inputs for a phone number( the inputs split he phone number into area code, first three digits and the following four digits) into a string fullPhoneNumber. I looked through a few answers before asking but I couldn't adapt them to my code.
fullPhoneNumber = areaCodeInput.getText().toString() + firstThreeDigitsInput.getText().toString() + finalFourDigitsInput.getText().toString();
Next this code enters the full string into a database.
LoginEntries newPhoneNumber = new LoginEntries("", "", "", "","", fullPhoneNumber);
This piece of code is called when i want to print the phone number in an activity. I use substring in this method the display the phone number in the format(888)-888-8888. But there is always an error. dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 3) + '-' + holder.substring(7, 4);
public String phoneNumberDatabaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";
//cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 3) + '-' + holder.substring(7, 4);
dbString += "\n";
}
c.moveToNext();
}
db.close();
c.close();
return dbString;
}
This is the error if I try the substring as I found it on stackoverflow.
Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 17397
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
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.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191) at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5972 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:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.Zyg`enter code here`oteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
I changed the code a bit hoping it would help but to no avail. dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 6) + '-' + holder.substring(7, 10);
public String phoneNumberDatabaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";
//cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 6) + '-' + holder.substring(7, 10);
dbString += "\n";
}
c.moveToNext();
}
db.close();
c.close();
return dbString;
}
This is the error I get from this particular code.
Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 11254
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
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.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
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:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
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:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Thanks for reading
You should read the method description carefully. String.substring (int start, int end) parameters are start and end, not start and length.
So using it as
holder.substring(4, 3) you try to take substring starting at 4th, and ending at 3rd char, the length of this substring naturally is -1
String index is 0 based, holder.substring( 1, 3) should be holder.substring(0, 3) etc. (The first parameter is inclusive and second parameter is exclusive to 0 - 3 means it will get index 0, 1 and 2)
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.