Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting Records into an Sqlite Database (Android) from Arrays
    primarykey
    data
    text
    <p>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.</p> <p>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 </p> <pre><code>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"};` </code></pre> <p>My For-Endfor comes next</p> <pre><code>'for(int i=1; i&lt;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(); </code></pre> <p>The Toast is there and it shows the cattleid incrementing correctly </p> <p><code>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); }</code> </p> <p>And this is my class</p> <pre><code>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; } } </code></pre> <p>Table Creation</p> <pre><code>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);"; </code></pre> <p>Any suggestions would be appreciated. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload