'Android studio won't create SQLite table
I have been getting error messages in the log saying that "columns" are not created whenever I try to insert a username and password. I need fresh sets of eyes to check my code! What am I doing wrong?
Thank you in advance!!
This is the MainActivity:
package com.vogella.android.sqlitedatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
//Initialaize stuff
EditText editTextID;
EditText editTextName;
EditText editTextPassword;
DatabaseManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Access corresponding edittext
editTextID = (EditText) findViewById(R.id.editTextID);
editTextName = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
//Open database here
dbManager = new DatabaseManager(this);
try {
dbManager.open();
}
catch (Exception e){
e.printStackTrace();
}
}
public void btnInsert(View view) {
dbManager.insert(editTextName.getText().toString(), editTextPassword.getText().toString());
}
public void btnFetch(View view) {
Cursor cursor= dbManager.fetch();
if(cursor.moveToFirst()) {
do {
String ID= cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.User_ID));
String username = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.User_Name));
String password = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.User_Pass));
Log.i("DATABASE_TAG", "I have read ID : " + ID + "Username: " + "password: " + password);
}while (cursor.moveToNext());
}
}
public void btnUpdate(View view) {
dbManager.update(Long.parseLong(editTextID.getText().toString()), editTextName.getText().toString(), editTextPassword.getText().toString());
}
public void btnDelete(View view) {
dbManager.delete(Long.parseLong(editTextID.getText().toString()));
}
}
This is the DatabaseManager:
package com.vogella.android.sqlitedatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.SQLDataException;
public class DatabaseManager {
private DatabaseHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DatabaseManager(Context ctx){
context= ctx;
}
//Initialize database instance
public DatabaseManager open() throws SQLDataException {
dbHelper = new DatabaseHelper(context);
database= dbHelper.getWritableDatabase();
return this;
}
//Close database
public void close(){
dbHelper.close();
}
public void insert (String username, String password){
ContentValues contentValues= new ContentValues();
contentValues.put(DatabaseHelper.User_Name, username);
contentValues.put(DatabaseHelper.User_Pass, password);
database.insert(DatabaseHelper.Database_Table, null, contentValues);
}
public Cursor fetch(){
String [] columns = new String [] {DatabaseHelper.User_ID, DatabaseHelper.User_Name, DatabaseHelper.User_Pass};
Cursor cursor= database.query(DatabaseHelper.Database_Table, columns, null, null, null,
null, null);
if (cursor!=null){
cursor.moveToFirst();
}
return cursor;
}
public int update (long _id, String username, String password){
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.User_Name, username);
contentValues.put(DatabaseHelper.User_Pass, password);
int ret= database.update(DatabaseHelper.Database_Table, contentValues, DatabaseHelper.User_ID+"="+_id,
null);
return ret;
}
public void delete(long _id){
database.delete(DatabaseHelper.Database_Table, DatabaseHelper.User_ID+"="+_id, null);
}
}
This is the DatabaseHelper:
package com.vogella.android.sqlitedatabase;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String Database_name= "MY_COMPANY.DB";
static final int Database_version= 1;
static final String Database_Table= "USERS";
static final String User_ID= "_ID";
static final String User_Name= "user_name";
static final String User_Pass= "password";
//Creating query for database
private static final String CREATE_DB_QUERY= "CREATE TABLE " + Database_Table + " ( " + User_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ User_Name + " TEXT NOT NULL, " + User_Pass + " TEXT NOT NULL );";
public DatabaseHelper(Context context) {
super(context, Database_name, null, Database_version);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_DB_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ Database_Table);
}
}
And these are the error messages I'm getting:
2022-02-24 15:03:11.249 13154-13154/com.vogella.android.sqlitedatabase E/SQLiteLog: (1) table USERS has no column named password
2022-02-24 15:03:11.261 13154-13154/com.vogella.android.sqlitedatabase E/SQLiteDatabase: Error inserting password=lkj user_name=testing
android.database.sqlite.SQLiteException: table USERS has no column named password (code 1): , while compiling: INSERT INTO USERS(password,user_name) VALUES (?,?)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
