Android.database.sqlite.SQLiteException: no such column: 500.0 (code 1) - android

Since 2 days, I'm not able to find the mistake. I get this error if I click on a FloatingActionButton:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.pizza, PID: 32058
android.database.sqlite.SQLiteException: no such column: 500.0 (code 1): , while compiling: INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('26','Margarita','1','500'.'0');
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: 500.0 (code 1): , while compiling: INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('26','Margarita','1','500'.'0');)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
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.executeSql(SQLiteDatabase.java:2109)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2039)
at de.pizza.Database.Database.addToCart(Database.java:60)
at de.pizza.FoodDetail$1.onClick(FoodDetail.java:57)
at android.view.View.performClick(View.java:6897)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
I checked my query more than 10 times and can't find anything :(
FoodDetail.java
public List<Order> getCarts() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
String sqlTable = "OrderDetail";
qb.setTables(sqlTable);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
final List<Order> result = new ArrayList<>();
if (c.moveToFirst()) {
do {
result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
c.getString(c.getColumnIndex("ProductName")),
c.getString(c.getColumnIndex("Quantity")),
c.getString(c.getColumnIndex("Price")),
c.getString(c.getColumnIndex("Discount"))
));
} while (c.moveToNext());
}
return result;
}
Database.java
public class Database extends SQLiteAssetHelper {
private static final String DB_NAME = "PBBANK.db";
private static int DB_VER = 1;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
}
public List<Order> getCarts() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
String sqlTable = "OrderDetail";
qb.setTables(sqlTable);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
final List<Order> result = new ArrayList<>();
if (c.moveToFirst()) {
do {
result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
c.getString(c.getColumnIndex("ProductName")),
c.getString(c.getColumnIndex("Quantity")),
c.getString(c.getColumnIndex("Price")),
c.getString(c.getColumnIndex("Discount"))
));
} while (c.moveToNext());
}
return result;
}
public void addToCart(Order order)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s'.'%s');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
db.execSQL(query);
}
public void cleanCart()
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM ORDER OrderDetail");
db.execSQL(query);
}
}
Screenshot of my SQLite Database
I hope that my information is sufficient, and somebody can help me.
Thanks!

You have a problem inside your SQL query. Your last param has not comma. Change your line to:
From
'%s','%s','%s','%s'.'%s'
To
'%s','%s','%s','%s','%s'
Hope it will help you. Let me know.

Your issue is that you have a period rather than a comma separating the values for price and discount and thus that SQLite interprets this as being a column.
The quick fix would be to change :-
String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s'.'%s');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
to
String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s','%s');", //<<<<<<<<<< CHANGED
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
However using the SQLiteDatabase insert method would reduce the chance of such issues, so the above could be :-
public long addToCart(Order order)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("ProductId",order.getProductId());
cv.put("ProductName",order.getProductName());
cv.put("Quantity",order.getQuantity());
cv.put("Price",order.getPrice());
cv.put("Discount",order.getDiscount());
return db.insert("OrderDetail",null,cv);
}
The insert convenience method has the advantages that
the SQL is generated,
it captures and returns the result (the id of the inserted row or if the row wasn't inserted -1)
it uses INSERT OR IGNORE so will not fail (bit will return -1) if a constraint conflict (UNIQUE or NOT NULL) occurs.

Related

how to search long type in database with cursor

i want to before add data to table be sure studentId Not the same with another studentId but i don't know how to search long type with cursor
my code :
public void SaveStudent(Long studentId , int classID , String studentName)
{
String stdId = studentId.toString();
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{stdId});
if (cursor.getCount() == 0)
{
ContentValues values = new ContentValues();
values.put("student_id", studentId);
values.put("class_id", classID);
values.put("student_name", studentName);
database.insert("student", null, values);
Toast.makeText(context, "save!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "StudentID is duplicate", Toast.LENGTH_SHORT).show();
}
}
logcat error :
02-23 21:37:39.108 26986-26986/com.example.user.classmanager E/SQLiteLog: (1) near "=": syntax error
02-23 21:37:39.110 26986-26986/com.example.user.classmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.classmanager, PID: 26986
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from studentwhere student_id = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at com.example.user.classmanager.DatabaseHandler.SaveStudent(DatabaseHandler.java:184)
at com.example.user.classmanager.SecondTab$1$1$1.onClick(SecondTab.java:97)
Try the following
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{String.valueOf(stdId)});

Android Studio : E/SQLiteLog: (1) table places has no column named description_place

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

how to update sqlite db in Android?

I want to update one row based on multiple row value, After run the application getting error like = ( FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE Tablename SET sFollowed=?,Code=?,Name=? WHERE Vessel_Name= )
Here is my update method in MyDbhelper class
public boolean update_isFollowed(String strVesselName , String strVesselNotationNumber, String strIsFollowed) {
SQLiteDatabase db1 = getReadableDatabase();
ContentValues values=new ContentValues();
values.put(COL_VesselName,strVesselName );
values.put(COL_ClassCodeNumber , strVesselNotationNumber);
values.put(COL_IsFollowedVessels, strIsFollowed);
int id=db1.update(Table_VesselList,values,COL_ClassCodeNumber + "=" +strVesselNotationNumber +" and "+ COL_VesselName +"="+strVesselName,null);
return id > 0;
}
Do something like this
In your activity
private UpdateDataInDB mUpdateDataInDb;
mUpdateDataInDb = new UpdateDataInDB(activity);
// call the update method with updated strings
mUpdateDataInDb.updateCommentDetails(_name, heading, comment);
In Database class
public void updateCommentDetails(String name, String updatedHeading,
String updatedComment) {
// TODO Auto-generated method stub
mDatabase = mDatabase_handler.getReadableDatabase();
ContentValues args = new ContentValues();
args.put("Heading", updatedHeading);
args.put("Comment", updatedComment);
mDatabase.update("AddCommentTable", args,"Name" + "= ?", new String[]{ name });
args.clear();
}
Use proper where condition string with values as argument. like:
String where = COL_ClassCodeNumber+"=? AND "+COL_VesselName="?";
String[] whereArgs = new String[] {String.valueOf(strVesselNotationNumber),
String.valueOf(strVesselName)};
int id=db1.update(Table_VesselList,values,where,whereArgs);

SQLite no such column error

Hi can anyone please help me with below error in android sqlite ? really appreciate!
Caused by: android.database.sqlite.SQLiteException: no such column: House (code 1): , while compiling: select * from category where category =House
below is part of my code in which I have inserted "House" in the table
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORY_TABLE = "CREATE TABLE category( " +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"category TEXT UNIQUE)";
db.execSQL(CREATE_CATEGORY_TABLE);
}
public void addCategory(String name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("category", name);
db.insert(CATEGORY_TABLE, // table
null, //nullColumnHack
cv); // key/value -> keys = column names/ values = column values
db.close();}
public List getCategory(){
List<String> list=new LinkedList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor =
db.rawQuery("select * from category where category =house" , null);
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
do {
String s = (cursor.getString(1));
list.add(s);
}while (cursor.moveToNext());
return list;
}
You need to wrap house with single quotes
db.rawQuery("select * from category where category = 'house'" , null);
In my case error was in trigger I wrote on table insert and update

java null pointer exception at at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)

public String getValue()
{
SQLiteDatabase db=getReadableDatabase();
String sql = String.format("SELECT %sFROM %s ", NUMBER_PASSWD,PASSWORD_TABLE);
db.close();
Log.d(MainActivity.DEBUG_TAG,"Sending Value");
return sql;
}
I am having a problem with this part of the code where I am trying to check the values in the database and retrieve it
Your_sqliteadminClass admin = new Your_sqliteadminClass(this, name_db, null,
version_db);
SQLiteDatabase db=admin.getReadableDatabase();

Categories

Resources