I am developing my first application which contains an SQLite database, containing table ANIMAL which has the columns _id, animal_name, animal_bio.
The user is presented with animal names in a ListView; selecting an animal will bring him/her to a page where he/she can view the animals bio.
The problem I'm having is:
When I add the bio of each animal to the DB - no errors.
However, running the app causes the ListView (previously working) to display a blank screen.
My Insertion code:
public long populateDB(){
ContentValues initialValues = new ContentValues();
long[] rowIds = new long[animalName.length];
// Populate the animal table
for(int i = 0; i < animalName.length; i++){
initialValues.put(KEY_ANIMALNAME, animalName[i]);
initialValues.put(KEY_BIOGRAPHY, bio[i]);
rowIds[i] = qmDB.insert(ANIMAL_TABLE, null, initialValues);
}
return rowIds[0];
}
And the create database statement
private static final String DATABASE_CREATE =
"create table " + ANIMAL_TABLE +
" (_id integer primary key autoincrement, " +
"animal_name text not null, " +
"biography text not null);";
I cannot see anything wrong with this code, so if anyone has any suggestions, I'd be very grateful.
EDIT: Retrieving animals code
public Cursor retrieveAnnimals(){
return qmDB.query(ANIMAL_TABLE, new String[] {
KEY_ROWID,
KEY_ANIMALNAME,
},
null,
null,
null,
null,
ORDER_BY_NAME);
}
Calling the create and insert from application, this takes place in the ListActivity, called ListAnimals:
dbm = new MyDBManager();
dbm.open();
dbm.deleteTable();
dbm.populateTable();
myCursor = dbm.retrieveAnimals();
Related
I create a database containing 4 String columns in a separate class called CalDatabaseHelper:
public void onCreate(SQLiteDatabase db) {
updateDatabase(db,0,DATABASE_VERSION);
}
private static void updateDatabase(SQLiteDatabase db, int olderversion, int newerVersion){
if (olderversion < 1){
db.execSQL("CREATE TABLE CAL (_id TEXT PRIMARY KEY,"
+ "ACTIVITY1 TEXT, "
+ "ACTIVITY2 TEXT"
+ "ACTIVITY3 TEXT);");
}
}
private static void insertIntoDatabase(SQLiteDatabase db, String primaryKey, String activityOne, String activityTwo, String activityThree){
ContentValues values = new ContentValues();
values.put("_id",primaryKey);
values.put("ACTIVITY1",activityOne);
values.put("ACTIVITY2",activityTwo);
values.put("ACTIVITY3",activityThree);
db.insert("CAL",null,values);
}
I add data in an Activity called Appointments. For now, I just add to the _id (a String variable) and ACTIVITY1 (a String variable that comes from user input into and EditText) columns:
SQLiteOpenHelper sqLiteOpenHelper = new CalDatabaseHelper(Appointments.this);
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
values.put("_id",primaryKey);
values.put("ACTIVITY1", activityOne);
db.insert("CAL", null, values);
db.close();
I attempt to retrieve this data in an Adapter Class. Once a widget is clicked, a database is opened, a Cursor finds the two columns(_id, ACTIVITY1) and the data is retrieved. This class contains the primaryKey data that I use to search the database:
SQLiteOpenHelper sqLiteOpenHelper = new CalDatabaseHelper(context);
db = sqLiteOpenHelper.getReadableDatabase();
cursor = db.query("CAL",
new String[]{"_id","ACTIVITY1"},
"_id = ?",
new String[]{month_day_year},
null, null, null);
if (cursor.moveToFirst()){
String actOne = cursor.getString(0);
activityOne.setText(actOne);
}else{
Toast.makeText(context, "NOTHING FOUND DURING OPEN", Toast.LENGTH_LONG).show();
}
cursor.close();
db.close();
Up until this point, everything works fine. I am able to retrieve the data from the first column (_id) by using cursor.getString(0).
When I go to retrieve the data from the 2nd column (ACTIVITY1), I keep getting an empty String. For example, cursor.getString(1) returns "". This should be the data that my user inputted in my Appointments Activity. The data is clearly placed in to ContentValues within that class and then put in to the database. Any idea why nothing is coming up there? Is it because I am using db.insert() instead of the method I created in my databaseHelper class called insertIntoDatabase()? How come the primary key is inserted then anyway? Thank you
I have a table with 10 columns.
String createQuery = " CREATE TABLE IF NOT EXISTS profile (
_id integer primary key autoincrement,
name text,
longi real,
lati real,
vibration integer,
sound integer,
brightness integer,
mdata,
bluetooth,
wifi);";
How can I get all table data in an ArrayList?
You need to do a few things. You need to create a sub-class of SQLiteDatabase. Once you have that, you can get run a query inside a method in this class like this:
Cursor cursor = getReadableDatabase().query("profile", // table name
new String[] { // columns
"_id",
"name",
"longi",
"lati",
"vibration",
"sound",
"brightness",
"mdata",
"bluetooth",
"wifi"
},
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Long id = cursor.getLong("_id");
String name = cursor.getString("name");
// and so on, list of your columns you want to get.
cursor.moveToNext();
}
Obviously you won't be able to put one whole row into an array list because you have different column types. But you can process one row at a time inside the while loop after you get all the values.
Most of this info is taken from here, which is a great source of info:
http://www.vogella.com/articles/AndroidSQLite/article.html
I have got 2 tables. Headers with names and details with texts:
create table Headers (_id integer primary key autoincrement, name string);
create table Details (_id integer primary key autoincrement, id_headers integet, text string);
id_headers is the link to table Headers row (one-to-many).
I want to write a method to upgrade these tables. The first and the least case I know is to create a temp table copy of 1st and 2nd tables, create new structure and insert data into new structure.
But in this case all "id_headers to _id" relations will be lost.
How can I keep them in new structure, and the same time I want them to keep as "autoincrement".
SQLiteDatabase.insert returns the new _id. Insert the Headers table data first, creating a mapping of new _id's against _id's in temp data structure.
Now when you populate the Details table consult your map for the old id_headers value to get the new id_headers value.
private void migrate(SQLiteDatabase db){
ArrayList<Header> oldHeaders = new ArrayList<Header>();
ArrayList<Detail> oldDetails = new ArrayList<Detail>)();
HashMap<Long,Long> idMap = new HashMap<Long,Long>();
Cursor oldHeadersCurs = db.query("Headers", null, null, null, null, null, null);
oldHeadersCurs.moveToFirst();
//store the old header records
while (!oldHeadersCurs.isAfterLast()){
long oldId = oldHeadersCurs.getLong(oldHeadersCurs.getColumnIndex("_id"));
String name = oldHeadersCurs.getString(oldHeadersCurs.getColumnIndex("name"));
oldHeaders.put(new Header(oldId,name));
oldHeadersCurs.moveToNext();
}
//delete the headers table
db.execSQL("DROP TABLE Headers");
//create the new headers table
db.execSQL(CREATE_NEW_HEADERS_TABLE_STMT);
//insert the header records capturing the new id
for (Header header : oldHeaders){
ContentValues cv = new ContentValues();
cv.put("name", header.getName());
long newId = db.insert("Headers", null, cv);
idMap.put(header.getId(), newId); //mapping the old _id to the new
}
//store the old detail records
Cursor oldDetailsCurs = db.query("Details", null, null, null, null, null, null);
oldDetailsCurs.moveToFirst();
while (!oldDetailsCurs.isAfterLast()){
//btw text is a data type in sqlite, you need to rename this column
String text = oldDetailsCurs.getString(oldDetailsCurs.getColumnIndex("text"));
long oldHeaderId = oldDetailsCurs.getLong(oldDetailsCurs.getColumnIndex("id_headers"));
oldDetails.put(new Detail(text,oldHeaderId));
oldDetails.moveToNext();
}
//recreate details table
db.execSQL("DROP TABLE Details");
db.execSQL("CREATE_NEW_DETAILS_TABLE_STMT");
//insert the new detail records using the id map
for (Detail detail : oldDetails){
ContentValues cv = new ContentValues();
cv.put("text",detail.getText());
cv.put("id", idMap.get(detail.getHeaderId())); //retrieving the new _id based on the old
db.insert("Details", null, cv);
}
}
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.
I am trying to put together an SQL database but don't really know how to make it work. The intention is to have multiple columns, some with integers, some with strings in their cells. For this app, I want repetitions to be an integer and exercise to be a string. Here is the relevant parts of the code:
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_EXERCISE = "exercise";
public static final String KEY_REPS = "repetitions";
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_DATE + " text not null, "
+ KEY_EXERCISE + " text not null, "
+ KEY_REPS + " int not null, "
public long createExercise(String exercise, int reps) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_EXERCISE, exercise);
initialValues.put(KEY_REPS, reps);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
I put data in this table using test strings. Then I try to pull the data with the following query:
public Cursor graphQuery(String exercise, String workout) {
return mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS}, null, null,
null, null, null);
From there I try to put the data into a number array but it gives me an error. It tells me to put KEY_REPS as a number when I declared it. But if I declare KEY_REPS as a number it doesn't let me build my databes.
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{workoutDbAdapter.KEY_REPS}; //error here
I feel like I am missing a key part in how to create my database. Can anyone help?
Code from book I am trying to follow (except using integers) (from comment on first answer)
private void fillData() {
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
// Create an array to specify the fields we want (only the TITLE)
String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};
That being said, if someone knows of a good website that teaches SQLite as it applies to Android that would be awesome. The only ones I have been able to find are generic SQL sites and they aren't very helpful.
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{WorkoutDbAdapter.KEY_REPS}; //error here
This code here doesn't do what (I think) you want it to. You need to iterate over the cursor and get the data from there. I'm pretty sure, if you followed the Android sample code for using databases that WorkoutDbAdapter.KEY_REPS is a string constant that holds reps column name.
Try doing something like this:
List<Number> allReps = new ArrayList<Number>();
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
while (cursor.moveToNext()) {
int reps = cursor.getInt(cursor.getColumnIndexOrThrow(mDbHelper.KEY_REPS));
allReps.add(reps);
}
Number[] repsArray = allReps.toArray(new Number[]{});
// do stuff with repsArray and don't forget to close cursor