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();
Related
I have got a database table with a column "id INTEGER PRIMARY KEY AUTOINCREMENT". When I try to update an existing row of this table with the primary key, the app exits saying "Unfortunately is stopped."
public int updateUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, user.getUser_firstName()); // user first name
values.put(KEY_LASTNAME, user.getUser_lastName()); // user first name
values.put(KEY_USERNAME, user.getUsername());
values.put(KEY_PASSWORD, user.getPassword());
values.put(KEY_USERLEVEL, user.getUser_level());
values.put(KEY_DESIGNATION, user.getDesignation());
values.put(KEY_MOBILENO, user.getMobile_no());
values.put(KEY_EMAIL, user.getEmail());
values.put(KEY_NIC, user.getNIC());
values.put(KEY_GENDER, user.getGender());
values.put(KEY_DOB, user.getDOB());
values.put(KEY_AVAILABILITY, user.getAvailability());
db.close();
// updating row
return db.update(TABLE_USERS, values, KEY_ID + " = ?",
new String[] { String.valueOf(user.getUser_id()) });
Logcat :
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projectmanagementsys.com.sasith.project_management/com.projectmanagementsys.com.sasith.project_management.AddUser}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.projectmanagementsys.com.sasith.project_management/databases/ProjectManagement
There is no error in the whole thing and the code works well when I comment the update query. Can someone give a solution please ?
Thank you.
You have the line db.close() followed by db.update() and your error states that you are attempting to open an already-closed object. You haven't closed the db after you've added the data, you've closed it before. You'll need to update the row, close the database, then return your value.
// updating row
int result = db.update(TABLE_USERS, values, KEY_ID + " = ?",
new String[] { String.valueOf(user.getUser_id()) });
db.close();
return result;
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'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)" ;
I have used a separate class for creation of db. In that I have written the delete function like this
public void name_delete(String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_NAME + "=" + name, null);
//KEY_NAME is a column name
}
In main class I called this function
db.name_delete(""+all_names.getSelectedItem().toString());
all_names.getSelectedItem().toString() is a spinner selected item. To delete the particular row with the name selected in spinner. Help me how to write the function.
Should be
db.delete(TABLE_NAME, KEY_NAME + "=?", new String[]{name});
Also
db.name_delete(all_names.getSelectedItem().toString());
public void name_delete(String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_NAME +"=?", new String[]{name});
}
USE this
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