0

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

5
  • Why are you using the same id for all records? mCattleId = (long)-1 this doesn't sound right... Commented Jan 23, 2012 at 21:39
  • Post your db creation statement as well. Commented Jan 23, 2012 at 21:41
  • The mCattleid = is for auto-incrementing the records the db creation is as follows private 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);"; Commented Jan 23, 2012 at 21:47
  • For some reason it won't let me format my code on the table creation Commented Jan 23, 2012 at 21:49
  • If its autoincrementing then there I no point to even assign it. Since its already incrementing as you add new cattle. Commented Jan 23, 2012 at 23:09

1 Answer 1

1

You missed a comma in your create table code

" sex TEXT " should be " sex TEXT, "

Try that out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.