I'm getting that exception when I do an insert in my SQLite database
The following code gives me the exception:
mDbHelper.createUser("pablo","a","a","a","a");
The code from mDbHelper (MyDbAdapter):
private static final String USER_TABLE_CREATE = "CREATE TABLE user ( email varchar, password varchar, fullName varchar, mobilePhone varchar, mobileOperatingSystem varchar, PRIMARY KEY (email))";
public long createUser(String email, String password, String fullName, String mobilePhone, String mobileOperatingSystem)
{
ContentValues initialValues = new ContentValues();
initialValues.put("email",email);
initialValues.put("password",password);
initialValues.put("fullName",fullName);
initialValues.put("mobilePhone",mobilePhone);
initialValues.put("mobileOperatingSystem",mobileOperatingSystem);
return mDb.insert("user", null, initialValues);
}
The exception is created on the last line: return mDb.insert("user", null, initialValues);
You are inserting a duplicate email.
Plus the recommended way is to have a _ID column as primary key, even if you don't use it. This way on future uses, like use in a Adapter or List, you won't have to workaround.
Related
I'm new to android and I created a database and I have an id as an integer primary key autoincrement
but I check on the email attribute when I register a new acc on the application, but I have a problem with the sql query that I can't put my hand on.
So I got this error today on this method that calls the name from the database using the email, and I hope you can tell me what's wrong!
E/MessageQueue-JNI: android.database.sqlite.SQLiteException: near ".": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT user_name FROM USER_TABLE WHERE user_email = androidx.appcompat.widget.AppCompatEditText{3c496d9 VFED..CL. ........ 0,0-656,127 #7f0a0068 app:id/email}
Here's the method:
public String getName(String mail) {
String nameReturn="Name";
SQLiteDatabase db=getWritableDatabase();
Cursor cursor=db.rawQuery("SELECT user_name FROM USER_TABLE WHERE user_email = "+mail, null);
if(cursor.moveToFirst()) nameReturn = cursor.getString(0);
return nameReturn.toString();
}
Looks like you are passing the EditText view to the query. Just pass the text itself
editTextView.getText()
Because your condition is string. So you must set into single quote
Cursor cursor=db.rawQuery("SELECT user_name FROM USER_TABLE WHERE user_email = '"+mail +"'", null);
Or you can use this type:
String[] args = {mail};
String sql = "SELECT user_name FROM USER_TABLE WHERE user_email =?";
Cursor cursor= db.rawQuery(sql, args);
This is my code for table creation
database.execSQL("CREATE TABLE IF NOT EXISTS RNOTIFICATIONS(Position VARCHAR PRIMARY KEY NOT NULL,message VARCHAR, title VARCHAR, iconof VARCHAR);");
and insert data function is
public void insert_new_item(String n_message, String n_title,String n_icon) {
SQLiteDatabase database = this.getWritableDatabase();
String rows= String.valueOf((int)DatabaseUtils.queryNumEntries(database, "RNOTIFICATIONS"));
ContentValues values = new ContentValues();
values.put("Position",rows+1);
values.put("message", n_message);
values.put("title", n_title);
values.put("iconof",n_icon);
database.insert("RNOTIFICATIONS", null, values);
database.close();
}
INPUT: insert_new_item(hi,Instagram, 2)
OUTPUT: [{message=1, iconof=Instagram, title=hi}]
EXPECTED OUTPUT: [{Position=1,message=hi,title=Instagram, iconof=2}]
Insert command is inserting values in wrong columns. can anyone help. please
i have tried removing PRIMARY KEY and NOT NULL But still it does the error.
The problem was with wrong cursor position. It was (0)
map.put("message", cursor.getString(0));
It should have been (1)
map.put("message", cursor.getString(1));
I'm trying to create two tables in my android app database and the application crashes when i'm tring to insert values to the first table, but when i delete the database and recreate it only with one table, it works fine. i don't get it?
the code is:
private static String query_create_user = "CREATE TABLE User ( email TEXT PRIMARY KEY, firstName TEXT, lastName TEXT, password TEXT)";
private static String query_create_group = "CREATE TABLE Group ( groupNumber INTEGER PRIMARY KEY AUTOINCREMENT, courseNumber INTEGER, groupName TEXT, groupType TEXT, groupOwner TEXT)";
public SchoolBagDataBase (Context applicationcontext) {
super(applicationcontext, "contract.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(query_create_user);
database.execSQL(query_create_group);
}
thanks in advance!
The SQL statement held in string 'query_create_group' is invalid. You can't have a table called Group because GROUP is a reserved word in SQLite. Just choose a different name for your table.
I have a little Problem with an insert-Statement in my Android-App.
Here is the code:
public void addNote(Note noteItem, int modulNummer){
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_NOTE, noteItem.getNote());
cv.put(COLUMN_NOTEBESCHREIBUNG, noteItem.getBeschreibung());
cv.put(COLUMN_MODULID_FK, modulNummer);
db.insert(NOTETABLE, null, cv);
}
Now my problem. The first column in my table is an auto increment pk. And so i want to skip the first column and i want to begin the insert in the second column. How can i skip this first column?
Update
I've already deleted the .put for the first column. "COLUMN_NOTE" is my second column.
My table-structure looks like this:
id INTEGER AUTO INCREMENT
note double
beschreibung TEXT
modul_id INTEGER
UPDATE 2
I don't know why, but now it works. Thx for your help guys.
If you have a table like the following one:
private final String TAB_GROUP_ADD = "CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL);";
And you use the following insert command:
ContentValues values = new ContentValues();
values.put(K_TITLE, title);
values.put(K_DESCRIPTION, description);
db.insert(TAB_GROUP, null, values);
Everything should go fine. The primary key field "id" will no be filled in by Java and the SQLite Database will do it for you.
I inserted EditText value in database, Please help me how to get all the data in table and show it in a next Activity, please help me. I use the following code for inserting:
private static final String DATABASE_CREATE = "create table pill (id integer primary key autoincrement, "
+ "med VARCHAR, " + "dose1 VARCHAR,"+"dose2 VARCHAR,"+"dose3 VARCHAR);";
// ---insert data into the database---
public long insertData(String med, String dose1,String dose2,String dose3) {
ContentValues initialValues = new ContentValues();
initialValues.put(Med, med);
initialValues.put(Dose1, dose1);
initialValues.put(Dose2, dose2);
initialValues.put(Dose3, dose3);
return db.insert(DATABASE_TABLE, null, initialValues);
}
med, dose1, dose2, dose3 are the values from the EditText which is in another class.
You have to use:
db.query()
See that
Query only the first data from a table