Reputation: 519
I've searched for this but so far I haven't found the answer - I am trying to create a SQLite database with some sample data for my app.
When I do a single hard-coded record it works, but I want to put in about 10 records and was trying to use arrays to hold the data. Here's the code I use to create the arrays
String cattleid[]={"ID 01","ID 02","ID 03", "ID 04", "ID 05", "ID 06","ID 07","ID 09", "ID 10", "ID 11"};
String eartag[]={"01","02","03", "04", "05", "06","07","09", "10","11"};
String location[]={"East","East","East", "Hwy 16", "Hwy 16", "Pen 1","Pen 2","West", "West","East"};
String status[]={"A","A","A", "A", "A", "A","F","F", "F","A"};
String typeofanimal[]={"B","C","C", "C", "H", "S","S","A", "A","H"};
String sex[]={"M","F","F", "F", "F", "M","M","M", "F","F"};`
My For-Endfor comes next
'for(int i=1; i<11; i++){ Cattle cattlerecord = new Cattle(cattleid[i],eartag[i],location[i],status[i],typeofanimal[i],"","",sex[i],0,"Ranch_01");
Toast.makeText(AgBuildDatabases.this, cattleid[i], Toast.LENGTH_SHORT).show();
addCattle(cattlerecord);}'
mDatabase.setTransactionSuccessful();
The Toast is there and it shows the cattleid incrementing correctly
public void addCattle(Cattle newCattle){
ContentValues values = new ContentValues();
values.put("animal_id", newCattle.mAnimal);
values.put("eartag", newCattle.mEartag);
values.put("location", newCattle.mLocation);
values.put("status_of_animal", newCattle.mStatus);
values.put("type_of_animal", newCattle.mTypeOfAnimal);
values.put("dam", newCattle.mDam);
values.put("sire",newCattle.mSire);
values.put("sex", newCattle.mSex);
values.put("current_weight",newCattle.mCurrent);
values.put("ranch_id", newCattle.mRanchId);
newCattle.mCattleId = mDatabase.insert("tbl_cattle", null, values);
}
And this is my class
class Cattle {
String mAnimal;
String mEartag;
String mLocation;
String mStatus;
String mTypeOfAnimal;
String mDam;
String mSire;
String mSex;
int mCurrent;
String mRanchId;
Long mCattleId;
public Cattle(String animalid, String eartag, String location, String statusofanimal, String typeofanimal, String dam, String sire, String sex, int currentweight, String ranchid){
mAnimal = animalid;
mEartag = eartag;
mLocation = location;
mStatus = statusofanimal;
mTypeOfAnimal = typeofanimal;
mDam = dam;
mSire = sire;
mSex = sex;
mCurrent = currentweight;
mRanchId = ranchid;
mCattleId = (long) -1;
}
}
Table Creation
rivate static final String CREATE_CATTLE_TABLE = "CREATE TABLE tbl_cattle (id INTEGER PRIMARY KEY AUTOINCREMENT ," + " animal_id TEXT," + " eartag TEXT, " + " location TEXT, " + " status_of_animal TEXT," + " type_of_animal TEXT," + " dam TEXT," + " sire TEXT," + " sex TEXT " + " current_weight INT, " + "ranch_id TEXT);";
Any suggestions would be appreciated. Thanks
Upvotes: 0
Views: 786
Reputation: 22306
You missed a comma in your create table code
" sex TEXT " should be " sex TEXT, "
Try that out.
Upvotes: 1