The error I'm getting is SQLiteException: near ",": syntax error (code 1), and I cant figure where my mistake is.
I´ve read some other questions with the same error and the answer they give is that it's a simple syntax error with a missing comma. However I've checked again and that doesn't seem to be my problem. I've included the code below.
public static final String prospectos="RegProspectos.db";
public static final int dbversion = 1;
public static final String tabla_prospectos="prospectos";
public static final String cm_NomProspec="NomProspec";
public static final String cm_ApProspec="ApProspec";
public static final String cm_AmProspec="AmProspec";
public static final String cm_RfcProspec="RfcProspec";
public static final String cm_TelCasa="TelCasa";
public static final String cm_TelOficina="TelOficina";
public adminProspec(Context context)
{super(context,prospectos, null, dbversion);}
#Override
public void onCreate(SQLiteDatabase db) {
final String crea_tabla=
"CREATE TABLE "+tabla_prospectos+"("
+cm_NomProspec+ " TEXT,"
+cm_ApProspec+ " TEXT,"
+cm_AmProspec+ " TEXT,"
+cm_RfcProspec + " TEXT,"
+cm_TelCasa+ " TEXT,"
+cm_TelOficina+ " TEXT,"
+cm_UsrActReg+ " TEXT "+")";
db.execSQL(crea_tabla); }
Here is my logcat output:
06-30 11:15:18.083: E/SQLiteDatabase(1867): Error inserting = escobedo=escobedo gurrola=gurrola cesar=cesar Guec=Guec
06-30 11:15:18.083: E/SQLiteDatabase(1867): android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: INSERT INTO prospectos(,escobedo,gurrola,cesar,Guec) VALUES (?,?,?,?,?)
06-30 11:15:18.083: E/SQLiteDatabase(1867): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-30 11:15:18.083: E/SQLiteDatabase(1867): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
thank you so much like you said the error where in the insert method,
the problem i think, was that I were trying to insert values instead of fields correct me if I'm wrong, that's simple but i don't know how to explain it...
...sNomProspec.getText().toString();
...sApProspec.getText().toString();
...sAmProspec.getText().toString()
altaProspecto.put(NomProspec, NomProspec);
altaProspecto.put(ApProspec, ApProspec);
altaProspecto.put(AmProspec, AmProspec);
altaProspecto.put("NomProspec", sNomProspec.getText().toString());
altaProspecto.put("ApProspec", sApProspec.getText().toString());
altaProspecto.put("AmProspec", sAmProspec.getText().toString());
I just added the quotes and changed I corrected and now it works thanks a lot =D
06-30 11:15:18.083: E/SQLiteDatabase(1867): android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: INSERT INTO prospectos(,escobedo,gurrola,cesar,Guec) VALUES (?,?,?,?,?)
This error is about an Insert that you do somewhere... You put a comma inside an Insert so your Insert should be like this :
INSERT INTO prospectos(escobedo,gurrola,cesar,Guec) VALUES (?,?,?,?,?)
Other issue that I'm seeing is that in your Insert you put 4 fields and on your values you put 5, you have to put the same values as fields you put. (I guess your ? values are something that you don't want to show to us, if it's not, you can't pass a ? to an Insert).
See SQLite - INSERT Query to know more about INSERT INTO.
You should use ContentValues to insert your data:
Example:
ContentValues contentValues = new ContentValues();
contentValues.put(cm_NomProspec, "nomProstect value");
contentValues.put(cm_ApProspec, "Prostect value");
contentValues.put(cm_RfcProspec, "pfProstect value");
database.insert(tabla_prospectos, null, contentValues);
bd.close();
Toast.makeText(this, "Se Cargaron los Datos del Prospecto,",+ Toast.LENGTH_LONG).show();
Related
I'm trying to insert rows in my Student Table which contains two rows : ID and Name
Here is the addHandler function which is implemented in MyDBHandler class :
public void addHandler(Student student) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, student.getID());
values.put(COLUMN_NAME, student.getStudentName());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
The onCreate method is :-
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT )";
db.execSQL(CREATE_TABLE);
}
The attributes of MyDBHandler class which extends SQLiteOpenHelper :
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "studentDB.db";
public static final String TABLE_NAME = "Student";
public static final String COLUMN_ID = "StudentID";
public static final String COLUMN_NAME = "StudentName";
I have a ADD Button in my activity_main.xml file and here is the code behind :
public void add(View view){
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
int id = Integer.parseInt(studentIdText.getText().toString());
String name = studentNameText.getText().toString();
Student student = new Student(id, name);
dbHandler.addHandler(student);
studentIdText.setText("");
studentNameText.setText("");
}
The app is running perfectly but when i want to insert a row in the table , i get the following errors in Run Tab :
E/SQLiteLog: (1) table Student has no column named StudentID
E/SQLiteDatabase: Error inserting StudentName=yassine StudentID=10
android.database.sqlite.SQLiteException: table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.example.test.MyDBHandler.addHandler(MyDBHandler.java:48)
at com.example.test.MainActivity.add(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10566)
at android.view.View$PerformClick.run(View.java:21256)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Any recommendations ?
Your issue is very likely a mis-conception in regard to the onCreate method. That is it onCreate doesn't run every time the App is run. onCreate will only run if the database doesn't actually exist. If the database has already been created by another previous run of the App then the onCreate method is not run.
As such any changes made to the structure of the database, as typically applied in the onCreate method will not be applied.
It would appear that you have added the defnition for the StudentId column, to the code in the onCreate method, after the App has been run.
As long as you have no data that needs to be preserved (which is very likely) then the simplest solution is to do 1 of the following :-
delete or clear the App's data (via settings/Apps)
uninstall the App
and then rerun the App, the database will then be created using the code as per the modified onCreate code.
I am trying to update the column in the table using update method. I am thrown syntax error like this:
SQLiteException: no such column: DataStructures (code 1): , while compiling: UPDATE tbl_courses SET checked_status=? WHERE course_name=DataStructures
Here is my Database Query:
public static final String TABLE_COURSES = "tbl_courses";
public static final String COLUMN_COURSE_ID = "_id";
public static final String COLUMN_COURSE_NAME = "course_name";
public static final String COLUMN_COURSE_SELECTED = "checked_status";
public static final String COLUMN_COURSE_CODE = "course_code";
public void updateSelectedCourseField(String courseName, String courseSelected) {
ContentValues args = new ContentValues();
args.put(COLUMN_COURSE_SELECTED, courseSelected);
database.update(TABLE_COURSES, args, COLUMN_COURSE_NAME + "=" + courseName,null);
}
I am trying to change checked_status value in tbl_courses where course_name = DataStructures. I am sure its in the database but its throwing me the error. please guide me through this.
A better solution is to avoid string concatenation to build your query. Instead do this:
database.update(TABLE_COURSES, args, COLUMN_COURSE_NAME + "=?", new String[]{courseName});
This will not only take care of the quotes for you, but it will also avoid SQL injection attacks. Always be careful when using user input in a SQL query.
I keep getting this error "table places has no clumn named description_place" still pretty new to android so no clue why this happens, can anyone spot the error?
This is my Database class:
public class BDManager {
private static final String TABLE_NAME = "places";
public static final String KEY_ID_PLACE = "_id";
public static final String KEY_NOM_PLACE = "nom_place";
public static final String KEY_TYPE_PLACE = "type_place";
public static final String KEY_ADDRESS_PLACE = "address_place";
public static final String KEY_DESCRIPTION_PLACE = "description_place";
public static final String CREATE_TABLE_PLACES = "CREATE TABLE "
+ TABLE_NAME + "("
+ KEY_ID_PLACE + " INTEGER PRIMARY KEY, "
+ KEY_NOM_PLACE +" TEXT, "
+ KEY_TYPE_PLACE +" TEXT, "
+ KEY_ADDRESS_PLACE +" TEXT, "
+ KEY_DESCRIPTION_PLACE +" TEXT);";
private MySQLite maBaseSQLite;
private SQLiteDatabase db;
public BDManager(Context context){
maBaseSQLite = MySQLite.getInstance(context);
}
public void open() {
db = maBaseSQLite.getWritableDatabase();
}
public void close() {
db.close();
}
public long addPlace(BD place){
ContentValues values = new ContentValues();
values.put(KEY_NOM_PLACE, place.getNom_place());
values.put(KEY_TYPE_PLACE, place.getType_place());
values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());
return db.insert(TABLE_NAME,null,values);
}
public int modPlace(BD place){
ContentValues values = new ContentValues();
values.put(KEY_NOM_PLACE, place.getNom_place());
values.put(KEY_TYPE_PLACE, place.getType_place());
values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());
String where = KEY_ID_PLACE+" = ?";
String[] whereArgs = {place.getId_place()+""};
return db.update(TABLE_NAME, values, where, whereArgs);
}
public BD getPlace(int id){
BD p = new BD("","","","");
Cursor c = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+KEY_ID_PLACE+"="+id, null);
if (c.moveToFirst()){
p.setId_place(c.getInt(c.getColumnIndex(KEY_ID_PLACE)));
p.setNom_place(c.getString(c.getColumnIndex(KEY_NOM_PLACE)));
p.setType_place(c.getString(c.getColumnIndex(KEY_TYPE_PLACE)));
p.setAddress_place(c.getString(c.getColumnIndex(KEY_ADDRESS_PLACE)));
p.setDescription_place(c.getString(c.getColumnIndex(KEY_DESCRIPTION_PLACE)));
c.close();
}
return p;
}
public Cursor getPlaces(){
return db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
}
}
It makes the error when I try tu save a place with the addplace function here :
public void savePlace(View view) {
final EditText name = (EditText) findViewById(R.id.NomPlace);
final Spinner type = (Spinner) findViewById(R.id.TypePlace);
final EditText address = (EditText) findViewById(R.id.AdressePlace);
final EditText description = (EditText) findViewById(R.id.DescriptionPlace);
BDManager sav = new BDManager(this);
sav.open();
sav.addPlace(new BD(name.getText().toString() ,type.getSelectedItem().toString() , address.getText().toString(),description.getText().toString()));
sav.close();
name.setText("");
type.setSelection(0);
address.setText("");
description.setText("");
}
Here is the complete error :
table places has no column named description_place
Error inserting description_place= nom_place=fyeyryfu address_place=dure type_place=Default
android.database.sqlite.SQLiteException: table places has no column named description_place (code 1): , while compiling: INSERT INTO places(description_place,nom_place,address_place,type_place) VALUES (?,?,?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table places has no column named description_place (code 1): , while compiling: INSERT INTO places(description_place,nom_place,address_place,type_place) VALUES (?,?,?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.myfavoriteplaces.myfavoriteplaces.BDManager.addPlace(BDManager.java:49)
at com.myfavoriteplaces.myfavoriteplaces.SavePlaces.savePlace(SavePlaces.java:124)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Can someone help me please ?
Dude i was gonna ask for a magic wand but then.... eureka (no nudity like the greek dude is involved :-) ) I once had similar problem
A SINGLE SPACE BETWEEN THE WORD "TEXT AND THE BRACE IS NEEDED
KEY_DESCRIPTION_PLACE +" TEXT );";
I trust AN UPVOTE IS THE LEAST U CAN OFFER
mostly when a missing column error is crushing android success dream... its the cause of typing error.
Heil Android!!!
when you change database information you need to Drop table in OnOpdate()
if you want shere your onCreate and OnUpdate (MySQLite) code and i fix it
Hi in my database i have a string like "computer's" , i am fetching it from database and displaying listview, when i click on the list item it should store to one of my database table. I am doing it as below but it is getting crashed
public void updateFav(String key, int value) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
C_FAV=C_FAV.replaceAll("'","''");
values.put(C_FAV, value);
db.update(tableName, values, C_KEY + "='" + key.trim() + "'", null);
db.close();
}
Below is my trace
12-12 17:55:49.291: E/AndroidRuntime(18184): android.database.sqlite.SQLiteException: near "s":
syntax error (code 1): , while compiling: UPDATE fav SET favorite=? WHERE key='computer's'
You're escaping the apostrophes in C_FAV which seems to be a column name but not in key.
Consider using ? placeholder and bind arguments instead, e.g.
db.update(tableName, values, C_KEY + "=?", new String[] { key.trim() });
I have a problem with SQLite in android , I tried a simple insert in a table but I have an exception and in the exception I read this :
INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)
this is my method :
public void AddBook(Bibliothèque bibliothèque)
{
SQLiteDatabase db=this.getWritableDatabase();
//this.open();
ContentValues values=new ContentValues();
values.put(KEY_Auteur,bibliothèque.getNomAuteur());
values.put(KEY_Livre,bibliothèque.getNomLivre());
values.put(Key_photo,bibliothèque.geturiimage().toString());
db.insert("Biblio",null,values);
db.close();
}
and this is my variables:
private static final String TABLE_Nom = "Biblio";
private static final String KEY_ID = "id";
private static final String KEY_Auteur = "Auteur";
private static final String KEY_Livre = "Livre";
private static final String Key_photo="Image";
the problem I don't know why but db.insert invert beetween Image and Livre and because of that I have an exception and I don't know why the value that I insert are ? ? ? in debug I can see that the value are correct ! . normaly the true expression must be :
INSERT INTO Biblio(Auteur,Livre,Image) VALUES (cc,test,(uri of picture))
this is the entire exception :
2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre
05-15 02:53:54.436 2927-2927/com.example.myapplication4.app E/SQLiteDatabase﹕ Error inserting Auteur=gsopfkgop Image=content://media/external/images/media/10 Livre=fhoksgkp
android.database.sqlite.SQLiteException: table Biblio has no column named Livre (code 1): , while compiling: INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)
USE This Query to Create Table.
db.execSQL
("CREATE TABLE Biblio (id INTEGER PRIMARY KEY AUTOINCREMENT ,Auteur TEXT,Livre TEXT,Image TEXT)");
then You have to insert record using Below code:
public void AddBook(Bibliothèque bibliothèque)
{
SQLiteDatabase db=this.getWritableDatabase();
//this.open();
ContentValues values=new ContentValues();
values.put(Auteur,bibliothèque.getNomAuteur());
values.put(Livre,bibliothèque.getNomLivre());
values.put(Image,bibliothèque.geturiimage().toString());
db.insert("Biblio",null,values);
db.close();
}
I hope its useful to you..
2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre
Your table doesn't have a column named Livre, either you have a spelling mistake here, in your table creation code, or you haven't defined a column named Livre.
You should try to reinstall your app?
Also, if your db schema has been changed after your app installed in your test device, you have to increase db version parameter to invoke onUpdate() method or reinstall your app to invoke onCreate().