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'
.is not allowed in a table or column name. Either change the name of the table to"users"or enclose the whole name inside square brackets:"[users.db]". Uninstall the app from the device and rerun.