This question already has answers here:
Why the database can not be known in Android SQLite?
(2 answers)
SQLiteException: unknown database
(1 answer)
unknown sqlite db error
(1 answer)
Closed 2 years ago.
I'm currently building my first app using an android studio that accesses an external database. However, the app is crashing and giving errors saying the database can not be opened. I've followed videos on youtube but I still have the problem.
When I try to debug the code I see that there is a database returned but, when it comes to the CREATE_TABLE_CMD command it fails.
Here is my main activity
public class MainActivity extends AppCompatActivity {
final String Table_Name="users.db";
final String CREATE_TABLE_CMD="CREATE TABLE IF NOT EXISTS "+Table_Name + "(id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT);";
SQLiteDatabase database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database=openOrCreateDatabase("database.sql",Context.MODE_PRIVATE,null);
database.execSQL(CREATE_TABLE_CMD);
ListView listView=findViewById(R.id.users_list);
final EditText edit_email=findViewById(R.id.top_edit);
final EditText edit_password=findViewById(R.id.enter_password);
ArrayAdapter<String> adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
Button add_btn=findViewById(R.id.add_button);
add_btn.setOnClickListener(v->{
String s1=edit_email.getText().toString();
String s2=edit_password.getText().toString();
ContentValues contentValues=new ContentValues();
contentValues.put("email",s1);
contentValues.put("password",s1);
database.insert(Table_Name,null,contentValues);
});
And my logcat
E/SQLiteLog: (1) unknown database users
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.databasetindog, PID: 10547
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasetindog/com.example.databasetindog.MainActivity}: android.database.sqlite.SQLiteException: unknown database users (code 1): , while compiling: CREATE TABLE IF NOT EXISTS users.db(id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.database.sqlite.SQLiteException: unknown database users (code 1): , while compiling: CREATE TABLE IF NOT EXISTS users.db(id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT);
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.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at com.example.databasetindog.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Disconnected from the target VM, address: 'localhost:8602', transport: 'socket'
Related
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());
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();
}
I have this errors:
Process: com.fusinato.lorenzo.colorglass, PID: 2257
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fusinato.lorenzo.colorglass/com.fusinato.lorenzo.colorglass.Main}: android.database.sqlite.SQLiteException: unrecognized token: "' prec int not null,tempo della presa precedente int not null);" (code 1): , while compiling: create table gran (_id integer primary key autoincrement, principale string,secondario string not null,media precedente int not null,quantita' prec int not null,tempo della presa precedente int not null);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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: unrecognized token: "' prec int not null,tempo della presa precedente int not null);" (code 1): , while compiling: create table gran (_id integer primary key autoincrement, principale string,secondario string not null,media precedente int not null,quantita' prec int not null,tempo della presa precedente int not null);
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.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.fusinato.lorenzo.colorglass.Main$GRANdb$SQLiteHelper.onCreate(Main.java:273)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.fusinato.lorenzo.colorglass.Main$GRANdb.openToWrite(Main.java:226)
at com.fusinato.lorenzo.colorglass.Main.onCreate(Main.java:53)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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)
This is the code.
I want to open the database and then check if it's empty or not.
If the database is empty, I want to populate the first 8 rows.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Granidb.openToWrite();
System.out.println("queueALL= " + Granidb.queueAll());
if (Granidb.queueAll().getCount() != 0) {
System.out.println("queueALL partito if= " + Granidb.queueAll().getCount());
//creagranigliatori();
}
Granidb.close();
and this are the method to open writable the database:
public GRANdb openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
I don't know why doesn't it work.
I'm a beginner in Android programming.
Your problem is... spaces in column names.
You can have spaces in your field names (if you REALLY want them), but only if you escape them.
i.e.:
[tempo della presa precedente] not null
Or simply get rid of spaces and use (for instance) underscores, instead:
i.e.:
tempo_della_presa_precedente not null
Let me suggest you that spaces aren't a great idea for column names.
Try the following:
"create table gran (" +
"_id integer primary key autoincrement, " +
"principale text, " +
"secondario text not null, " +
"media_precedente integer not null, " +
"quantita_prec integer not null, " +
"tempo_della_presa_precedente integer not null);"
Basically, you shouldn't have spaces in column names, and you should use TEXT for String column types and INTEGER or INT for int column types. [https://www.sqlite.org/datatype3.html]
You can use the following website to validate your SQL statements: http://sqlfiddle.com/
I agree with #FrankNStein that you should avoid spaces in column names, but you also had SQLite syntax errors.
im doing an application using an sqlite database
and im having an exception in this
public Prof getProffromcompte(int login){
Cursor c = bdd.query(TABLE_PROF, new String[] {COL_ID_PROF, COL_NOM_PROF, COL_LOGIN_PROF}, COL_LOGIN_PROF + " LIKE \"" + login +"\"", null, null, null, null);
return cursorToProf(c);
}
and this is th log :
Process: com.example.radouane.myapplication, PID: 14289
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radouane.myapplication/com.example.radouane.myapplication.activ2}: android.database.sqlite.SQLiteException: no such column: nom_prof (code 1): , while compiling: SELECT ID_prof, nom_prof, login_prof FROM table_prof WHERE login_prof LIKE "2"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: android.database.sqlite.SQLiteException: no such column: nom_prof (code 1): , while compiling: SELECT ID_prof, nom_prof, login_prof FROM table_prof WHERE login_prof LIKE "2"
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
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:1426)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1273)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1144)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1312)
at com.example.radouane.myapplication.BDD.getProffromcompte(BDD.java:195)
at com.example.radouane.myapplication.activ2.chooseModule(activ2.java:76)
at com.example.radouane.myapplication.activ2.onCreate(activ2.java:49)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
i verified that when creating the table i have the same same column name : nom_prof
Can you please Uninstall Application once from Device and Run again ?
If you have already created database and you have added new column than Database will not reflect right away on current table structure.
So after uninstalling application will create new instance of database with updated Data Structure.