qq_26614827
qq_26614827
2017-10-17 10:32

无法往sqlite插入数据

  • cursor
  • sqlite
  • 数据库

这里是相关的代码
onCreat()中
//数据库
dbHelper = new MyDatabaseHelper(this,"ASData.db",null,1000);//检测是否有数据库
dbHelper.getWritableDatabase();//无,则创建
初始化数据
PurchaseOrder purchase1 = new PurchaseOrder("AB11111111", "艾鲜农场", 22, "2017-10-11", "已完成");
mData.add(purchase1);
PurchaseOrder purchase2 = new PurchaseOrder("CD22222222", "哈哈农场", 18, "2017-10-12", "已入库");
mData.add(purchase2);
PurchaseOrder purchase3 = new PurchaseOrder("EF33333333", "百合农场", 6, "2017-10-12", "已审核");
mData.add(purchase3);
PurchaseOrder purchase4 = new PurchaseOrder("GH44444444", "艾鲜农场", 11, "2017-10-13", "未审核");
mData.add(purchase4);

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.delete("PurchaseOrder",null,null);
    ContentValues values = new ContentValues();
    for (int position = 0;position < mData.size();position ++) {
        values.put("purchaseId",mData.get(position).getPurchaseId());
        values.put("provider",mData.get(position).getProvider());
        values.put("orderCount",mData.get(position).getOrderCount());
        values.put("operateTime",mData.get(position).getOperateTime());
        values.put("purchaseState",mData.get(position).getPurchaseState());
        db.insert("PurchaseOrder",null,values);
        values.clear();
    }

这里我怀疑数据没有插入到表中
循环查询表
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("PurchaseOrder",null,null,null,null,null,null);
PurchaseOrder purchaseorder;
Boolean haha = cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
purchaseorder = new PurchaseOrder();//创建一个新对象
purchaseorder.setPurchaseId(cursor.getString(cursor.getColumnIndex("purchaseId")));
purchaseorder.setProvider(cursor.getString(cursor.getColumnIndex("provider")));
purchaseorder.setOrderCount(cursor.getInt(cursor.getColumnIndex("orderCount")));
purchaseorder.setOperateTime(cursor.getString(cursor.getColumnIndex("operateTime")));
purchaseorder.setPurchaseState(cursor.getString(cursor.getColumnIndex("purchaseState")));
mDataWhole.add(purchaseorder);
} while (cursor.moveToNext());
cursor.close();
}
最最关键的问题是,在另外 一个activity中,这些思路是可行的。
这里再附上MyDatabaseHelper的代码:
public class MyDatabaseHelper extends SQLiteOpenHelper {

private String TAG = "Database ";

public static final String CREATE_PLANPRODUCT = "create table PlanProduct ("
        +"planId integer primary key autoincrement, "
        +"productName text, "
        +"unit text, "
        +"purchaseNum text, "
        +"planTime text, "
        +"planState text, "
        +"provider text)";

public static final String CREATE_PURCHASEORDER = "create table PurchaseOrder ("
        +"purchaseId integer primary key autoincrement, "
        +"provider text, "
        +"orderCount text, "
        +"operateTime text, "
        +"purchaseState text)";

private Context mContext;

public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
    mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_PLANPRODUCT);
    db.execSQL(CREATE_PURCHASEORDER);
    //Log.d(TAG,"Create succeeded");
    Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists PlanProduct");
    db.execSQL("drop table if exists PurchaseOrder");
    onCreate(db);
}

}
拜托各位大佬啦,谢谢!!!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答