I'm facing BIG and strange problem , i have sqlite android database app on many mobile phones what is works perfectly , but when uses my app on lenovo or asus 7 inch tablet I'm facing no records stored in database , so i think maybe there some problems with getting current data with local.getdefualt()
here is my code :
INSERT statment :
public long addCheckPoint(String fleetNumber, String NoOFStudents,String Location_Longitude,String Location_Latitude){
SQLiteDatabase db = this.getWritableDatabase();
String ActionDate = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss",Locale.getDefault()).format(new Date());
ContentValues values = new ContentValues();
values.put("FleetNumber", fleetNumber);
values.put("CheckPointID", CheckPoint(fleetNumber));
values.put("NoOFStudents", NoOFStudents);
values.put("Location_Longitude", Location_Longitude);
values.put("Location_Latitude", Location_Latitude);
values.put("ActionDate", ActionDate);
values.put("PathStatus", "Open");
return db.insert("CheckPoints",null, values);
}
getting result statment :
public Cursor getCheckPointRows(String fleetNumber){
SQLiteDatabase db = this.getWritableDatabase();
String ActionDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String qq = "select CheckPointID , FleetNumber, NoOFStudents , ActionDate from CheckPoints where FleetNumber = "+ fleetNumber +" and date(ActionDate)='"+ActionDate+"'" ;
Cursor cursor = db.rawQuery(qq, null);
if(cursor != null && cursor.moveToFirst()){
return cursor;
}
db.close();
return cursor;
}
table strc
// SQL statement to create CheckPoints table
String CREATE_CheckPoints_TABLE = "CREATE TABLE CheckPoints ( " +
"ID INTEGER PRIMARY KEY AUTOINCREMENT , " +
"FleetNumber INTEGER, "+
"CheckPointID INTEGER,"+
"NoOFStudents INTEGER,"+
"Location_Longitude TEXT,"+
"Location_Latitude TEXT,"+
"PathStatus TEXT,"+
"ActionDate TEXT)" ;
Related
I am using below code to update database and putting where clause for date
ContentValues values = new ContentValues();
values.put(DBTransactions.USER_ID_KEY, userId);
values.put(DBTransactions.DATE_KEY, date);
values.put(DBTransactions.DAY_KEY, day);
values.put(DBTransactions.GOAL_KEY, dailyGoal);
values.put(DBTransactions.VOLUME_KEY, dailyVolume);
values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);
db.getWritableDatabase().update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ?", new String[]{date});
closeDB();
am I wrong somewhere in putting where clause? Its not updating database, Its creating new entry in database.
Its not updating database.
Thanks in Advance
I am using this code for update me Database and it's work fine , not a issue with me .
public void updateItemToCartTable(String itemId, String subitemid, String cost, int quantity) {
sdb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ITEM_COST, cost);
values.put(ITEM_QUANTITY, quantity);
Log.d("DB", "Count : " + quantity);
this.sdb.update(TABLE_NAME, values, ITEM_ID + " ='" + itemId + "' AND " + SUB_ITEM_ID + " ='" + subitemid + "'", null);
sdb.close();
}
Use This Code For Updating your Database
Use below code to update your table:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBTransactions.USER_ID_KEY, userId);
values.put(DBTransactions.DATE_KEY, date);
values.put(DBTransactions.DAY_KEY, day);
values.put(DBTransactions.GOAL_KEY, dailyGoal);
values.put(DBTransactions.VOLUME_KEY, dailyVolume);
values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);
db.update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ? ", new String[]{String.valueOf(date)});
closeDB();
What is data type of DBTransactions.DATE_KEY column in DBTransactions.TRANSACTION_TABLE_NAME table?
Insert data in local database and check the format in which date is inserting in DBTransactions.TRANSACTION_TABLE_NAME table.
While updating record based on DBTransactions.DATE_KEY condition, check DBTransactions.DATE_KEY matches value stored in DBTransactions.TRANSACTION_TABLE_NAME DBTransactions.DATE_KEY column.
Everything else in your code seems to be fine.
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
I'll try update database but it can't update. It insert correctly but don't change rows with new ones.
mydatabase = getMydatabase();// this.getWritableDatabase();
ContentValues devices = new ContentValues();
devices.put(KEY_NAME, name);
devices.put(KEY_ADDRESS, address);
devices.put(KEY_GRADE, grade);
devices.put(KEY_ARRAY,gradeArray);
try{
mydatabase.insertOrThrow(DATABASE_TABLE, null, devices);
} catch (SQLiteConstraintException e) {
String where = "'"+address+"'='"+KEY_ADDRESS+"'";
mydatabase.update(DATABASE_TABLE, devices, where,null);
}
mydatabase.close();
This code in function which take different grade and gradeArray values. They are strings.
Database create statement:
String create = String.format("CREATE TABLE %s ( %s INTEGER PRIMARY KEY AUTOINCREMENT,"+
" %s TEXT ,%s TEXT NOT NULL UNIQUE , %s TEXT NOT NULL, %s TEXT);",
DATABASE_TABLE, KEY_ROW_ID, KEY_NAME, KEY_ADDRESS, KEY_GRADE, KEY_ARRAY);
String where = "'"+address+"'='"+KEY_ADDRESS+"'";
You're comparing a literal to a literal here. Remove the '' from the column name, e.g.
String where = KEY_ADDRESS + "='"+address+"'";
To avoid SQL injection as such, use parameters:
String where = KEY_ADDRESS + "=?";
mydatabase.update(DATABASE_TABLE, devices, where, new String[] { address });
I have a table called app_catagory. The problem is, when I try to insert a row into the table, I got this exception:
FATAL EXCEPTION : main java.lang.NullPointerException
The LogCat shows this line as the offending code:
db.insert(DATABASE_TABLE2, null, initialValues);
How can I fix it?
Code
static final String DATABASE_APP_CAT= "create table app_catagory(cat_id interprimary key autoincrement, " +
"name text not null," +
"date_created date , " +
"img_name text);";
//Insert into catagory table
public static void insertContact_app_cat()
{
String[] name={"DOCTOR","PHARMACY","PATHLAB","BLOODBANK"};
for(int i=1;i<=4;i++){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CAT_NAME, name[i]);
initialValues1.put(KEY_CAT_DATE, "05-05-2014");
initialValues2.put(KEY_CAT_IMG,"null");
db.insert(DATABASE_TABLE2, null, initialValues);
}
}
I don't see where you are getting the db when I do an insert I call this piece of code before doing my ContentValues add stuff.
SQLiteDatabase db = this.getWritableDatabase();
Then after the insert I would close the database.
db.close();
I am trying to insert data in my sqlit database but I got android SQLiteConstraintException: error code 19: constraint failed exception. I saw there are tons of question in this topic, I've read and tried a bunch of them, but the exception still , i wonder if this exception caused by the auto increment food_id value , since the insert statement return -1 , and i wonder that why this exception occur since the first insert id done correctly but all the later inserting are failed , why this error occur ,and how i can solve it? please help me..
the create statements in the DBAdapter class
private static final String Meal_TABLE_CREATE= "create table IF NOT EXISTS Meal (Date text not null , "+
"Time text not null,MealType text not null,"+ " primary key(Date,Time ,MealType) );" ;
private static final String FOOD_TABLE_CREATE= "create table IF NOT EXISTS Food (_id INTEGER primary key AUTOINCREMENT , "+
"Food_Name text not null,Calories integer not null,"+ "VB12 integer not null,Cholesterol integer not null,"+
"Protein integer not null,Iron integer not null,Sodium integer not null,Fat_Mono integer not null,Fat_Sat integer not null,carbohydrate integer not null);" ;
private static final String MealFOOD_TABLE_CREATE= "create table IF NOT EXISTS MealFood (Date text not null , "+
"Time text not null,MealType text not null,"+"Food_ID integer not null , primary key(Date,Time ,MealType,Food_ID) );" ;
inserting methods
// insert meal to the meal table
public long SaveMeal(String date , String time , String mealType)
{
ContentValues content = new ContentValues();
content.put(KEY_MDATE,date);
content.put(KEY_MTIME,time);
content.put(KEY_MEALTYPE,mealType);
return db.insert(MEAL_TABLE_NAME, null, content);
}
// insert Food to the Food table
public long SaveFood(String name,int calories,int Vit_B12,int cholesterol,int protein ,int iron ,int sodium,int Fat_Mono,int Fat_Sat,int carbohydrate)
{
ContentValues content = new ContentValues();
content.put(KEY_FOODNAME,name);
content.put(KEY_CALORIES,calories);
content.put(KEY_VB12,Vit_B12);
content.put(KEY_CHOLESTEROL,cholesterol);
content.put(KEY_PROTEIN,protein);
content.put(KEY_IRON,iron);
content.put(KEY_SODIUM,sodium);
content.put(KEY_FAT_MONO,Fat_Mono);
content.put(KEY_FAT_Sat,Fat_Sat);
content.put(KEY_CARBOHYDRATE,carbohydrate);
return db.insert(FOOD_TABLE_NAME, null, content);
}
// get food id by its name
public int getFoodIDByName(String name) throws SQLException
{ int id;
Cursor cursor = null;
try{
cursor=db.query(true,FOOD_TABLE_NAME, new String[]{KEY_FOODID}, KEY_FOODNAME+ " = '" + name + "'", null, null, null, null,null);
if (cursor != null) {
cursor.moveToFirst();
}
id=0;
while (cursor.moveToNext())
id=cursor.getInt(cursor.getColumnIndex(KEY_FOODID));
}
finally{
cursor.close();
cursor.deactivate();
}
return id;
}
// insert mealFood to mealFood table
public long SaveMealFood(String date , String time , String mealType, int Food_id)
{
ContentValues content = new ContentValues();
content.put(KEY_MFDATE,date);
content.put(KEY_MFTIME,time);
content.put(KEY_MFMEALTYPE,mealType);
content.put(KEY_MFFOODID,Food_id);
return db.insert(MEALFOOD_TABLE_NAME, null, content);
}
java code
DBAdapter dbAdapter=new DBAdapter(SaveMeal.this);
dbAdapter.open();
Food n;
String m;
int FoodIDByName;
for(int i = 0; i <MealActivity.array.size(); i++){
m=MealActivity.array.get(i).toString();
Log.e("tag", m);//selected food name
for (int j = 0; j < MealActivity.tempList.size(); j++){
n=MealActivity.tempList.get(j);
if(n.getFOOD_NAME().equals(m)){
//save food
long food_id = dbAdapter.SaveFood(n.getFOOD_NAME(),n.getCALORIES(),n.getFOOD_VITAMIN_B12(),n.getCHOLESTEROL(),n.getFOOD_PROTEIN(),n.getFOOD_IRON(),n.getFOOD_SODIUM(),
n.getFOOD_MONO_UNSATURATED_FAT(),n.getFOOD_SATURATED_FAT(),n.getFOOD_TOTAL_CARBOHYDRATE());
Log.e("tag", food_id+" food inserting done");
//save meal
long meal_id= dbAdapter.SaveMeal( meal_date,meal_time,Meal.MEAL_TYPE);
Log.e("tag",meal_id+" meal inserting done");
//save meal_food
FoodIDByName=dbAdapter.getFoodIDByName(n.FOOD_NAME);
Log.e("tag",FoodIDByName+" food_id");
long meal_food_id=dbAdapter.SaveMealFood(meal_date,meal_time,Meal.MEAL_TYPE,FoodIDByName);
Log.e("tag",meal_food_id+" meal_food inserting done");
dbAdapter.close();
this result of this line Log.e("tag", food_id+" food inserting done"); in my log is -1
mylog
Database(657):at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
Database(657):at android.database.sqlite.SQLiteStatement.execute (SQLiteStatement.java:55)
Database(657):at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
-1 food inserting done
18 meal inserting done
0 food_id
13 meal_food inserting done
Try to remove all (Not NULL) constraints, and save the empty food.
If it is saved properly, try to add the constraint (NOT NULL) one by one.
I think one of the values is passed as NULL.
That error.means you are.violating a constraint (obviously). Most likely leave a "not null" column null.
You could also have violated your primary key by trying to save the same combination more than once.