@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
// providers data
// db.execSQL("insert into "
// + TABLE_PROVIDERS
// + " (_id, provider_id, parent_id, title, image, sh_n, enable, visible, image_bg, type, is_group, hide_in_search, limit_min, limit_max, off_on_line, invoice_search) "
// + "VALUES (1,600,101,'Источники оплаты','',1,1,0,'','',1,1,0,0,0,0)");
ContentValues cv = new ContentValues();
cv.put("_id", 1);
cv.put("provider_id", 600);
cv.put("parent_id", 101);
cv.put("title", "Bla");
cv.put("image", "bla");
cv.put("sh_n", 1);
cv.put("enable", 1);
cv.put("visible", 1);
cv.put("image_bg", "");
cv.put("type", "");
cv.put("is_group", 1);
cv.put("hide_in_search", 1);
cv.put("limit_min", 10);
cv.put("limit_max", 10000);
cv.put("off_on_line", 1);
cv.put("invoice_search", 1);
db.insertOrThrow(TABLE_PROVIDERS, null, cv);
}
现在我按照建议行事,逐行插入:
import java.util.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
public class ProviderDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public ProviderDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public List<Provider> getProvidersByParentId(long parentId) {
List<Provider> providersList = new ArrayList<Provider>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_PROVIDERS, new String[] { "provider_id", "title", "image" }, " parent_id=" + parentId,
null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Provider provider = cursorToProvider(cursor);
providersList.add(provider);
cursor.moveToNext();
}
cursor.close();
System.out.println("getProvidersByParentId");
return providersList;
}
private Provider cursorToProvider(Cursor cursor) {
Provider provider = new Provider();
provider.setId(cursor.getInt(1));
provider.setTitle(cursor.getString(3));
provider.setImg(cursor.getString(4));
return provider;
}
}
它不起作用。似乎没有插入行,并且getProvidersByParentId方法返回的数组为空。 parent_id作为参数是101.
答案 0 :(得分:4)
您的插入语句错误。在SQLite中,您不能通过用逗号分隔来插入多个记录,实际上您需要为此准备单独的插入命令。但是,如果您的SQLite版本 3.7.11 ,则可能。
阅读this。
答案 1 :(得分:0)
您可以使用像这样的for循环来完成
//添加按钮点击事件,如
String [] arr1 = new String [n]; String [] arr2 = new String [n]; String [] arr3 = new String [n];
addButton.setOnClickListener
(
new View.OnClickListener()
{
@Override public void onClick(View v) {
for(int i=1;1<4;i++){
try
{
// ask the database manager to add a row given the two strings
//db object of database manager class
db.addValues
(
arr1[i] ,
arr2[i] ,
arr3[i] ,
);
}
catch (Exception e)
{
Log.e("Add Error", e.toString());
e.printStackTrace();
}
}
);
//插入功能
public void addValues(String Col1, String Col2,
String col3)
{
ContentValues values = new ContentValues();
values.put(Columns1, Col1);
values.put(Columns2, Col2);
values.put(Columns3, Col3);
try{
db.insert(Course_Info_Table, null, values);
}
catch(Exception e)
{
}
}
答案 2 :(得分:0)
使用ContentValues
将数据插入到这样的表中。
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("_id", 1);
cv.put("provider_id", 600);
cv.put("parent_id", 0);
cv.put("title", "Источники оплаты");
cv.put("image", "");
cv.put("sh_n", 1);
cv.put("enable", 1);
cv.put("visible", 0);
cv.put("image_bg", "");
cv.put("type", "");
cv.put("is_group", 1);
cv.put("hide_in_search", 1);
cv.put("limit_min", 0);
cv.put("limit_max", 0);
cv.put("off_on_line", 0);
cv.put("invoice_search", 0);
db.insertOrThrow(TABLE_PROVIDERS, null, cv);
db.close();
多个记录的多次调用此方法。
public void insertMydata(int _id, int provider_id, int parent_id /*....add more parameters as you required */){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("_id", _id);
cv.put("provider_id", provider_id);
cv.put("parent_id", parent_id);
/*
......
.....
put more code here for other columns
......
*/
db.insertOrThrow(TABLE_PROVIDERS, null, cv);
db.close();
}