2012-01-23 103 views
0

我已經搜索了這個,但到目前爲止我還沒有找到答案 - 我試圖創建一個SQLite數據庫與我的應用程序的一些示例數據。從數組中插入記錄到Sqlite數據庫(Android)

當我做一個單一的硬編碼記錄它的工作原理,但我想放入約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"};` 

我換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();  

敬酒那裏的代碼,它顯示了cattleid正確遞增

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

任何建議,將不勝感激。 感謝

+0

爲什麼你對所有記錄使用相同的ID? mCattleId =(長)-1這聽起來不錯... – JoxTraex

+0

也發佈你的數據庫創建語句。 – JoxTraex

+0

的mCattleid =是自動遞增的記錄的數據庫創建如下 私有靜態最後絃樂CREATE_CATTLE_TABLE = 「CREATE TABLE tbl_cattle(ID INTEGER PRIMARY KEY AUTOINCREMENT,」 + \t \t \t 「animal_id TEXT,」 + \t \t \t 「耳標TEXT,」 + \t \t \t 「位置TEXT,」 + \t \t \t 「status_of_animal TEXT,」 + \t \t \t 「type_of_animal TEXT,」 + \t \t \t 「壩TEXT,」 + \t \t \t 「公畜TEXT,」 + \t \t \t 「性別TEXT」 + \t \t \t 「current_weight INT,」 + \t \t \t 「ranch_id TEXT);」; – Hank

回答

1

你錯過了在創建表的代碼

「性文本」 應該是 「性文本,」

逗號嘗試了這一點。