从Arrays将记录插入Sqlite数据库(Android)

时间:2012-01-23 21:11:14

标签: java android sqlite

我已经搜索了这个,但到目前为止我还没有找到答案 - 我正在尝试使用我的应用程序的一些示例数据创建一个SQLite数据库。

当我执行单个硬编码记录时,它可以工作,但我想要输入大约10条记录,并且尝试使用数组来保存数据。 这是我用来创建数组的代码

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"};` 

我的For-Endfor接下来

'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();    

Toast在那里,它显示了正确增加的内容

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); }

这是我的班级

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;
}
}

表格创建

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);";

任何建议将不胜感激。 感谢

1 个答案:

答案 0 :(得分:1)

您在创建表格代码中错过了逗号

“性别文字” 应该  “性别文字”,

尝试一下。