Issue
public class DBhandle {
private static final String DATABASE_NAME = "restaurantdatabase";
private static final int DATABASE_VERSION = 1;
final Context context;
private SQLiteDatabase ourDatabase;
//table name
private static final String LOGIN_TABLE_NAME = "Login";
//login table column name
public static final String KEY_ROWID = "ID";
public static final String A_NAME = "admin_name";
public static final String A_PHONE = "admin_phone";
DatabaseHelper dbHelper;
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + LOGIN_TABLE_NAME + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + A_NAME
+ " TEXT NOT NULL, " + A_PHONE + " TEXT NOT NULL);"
);
}
public long insertEntry(String userName, String password) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("A_NAME", userName);
newValues.put("A_PHONE", password);
// Insert the row into your table
return ourDatabase.insert(LOGIN_TABLE_NAME, null, newValues);
//Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
}
}
I tried to insert data into a database I receive the following exception:
android.database.sqlite.SQLiteException: table Login has no column named A_NAME (code 1): , while compiling: INSERT INTO Login(A_NAME,A_PHONE) VALUES (?,?).
Solution
You are referring to the columns "A_NAME" and "A_PHONE" as strings, when these are actually final static String constants, so the actual column names are "admin_name" and "admin_phone".
You need to change these two lines:
// Assign values for each row.
newValues.put("A_NAME", userName);
newValues.put("A_PHONE", password);
You have two options:
You either refer to the actual column names:
newValues.put("admin_name", userName);
newValues.put("admin_phone", password);
or use the static final constants without double quotes
newValues.put(A_NAME, userName);
newValues.put(A_PHONE, password);
Answered By - omerio