无法将多个项目列表视图插入到Sqlite中

时间:2014-01-23 04:54:10

标签: android sqlite listview

Im Beginner Android Developer。我需要一些帮助。当我尝试将数据插入2个表顺序和orderdtl时。 orderdtl从EntryTO.java上的多个项目列表视图中获取数据。没有变化,没有错误,仍然无法插入。这是我的数据库:

private static final String db_createorder = "create table  if not exists "
      + TABLE_ORDER + "("
      + ORDER_ID +" varchar(15) primary key, "
      + ORDER_DATE + " DATETIME, "
      + NOTES + " varchar(100) not null, "
      + OUTLET_ID + " varchar not null, " +
      "foreign key ("+ OUTLET_ID +") references "+TABLE_OUTLET+"("+ OUTLET_ID +"));";

private static final String db_createorderdtl = "create table  if not exists "
      + ORDER_DTL + "("
      + ORDER_ID +" varchar not null, "
      + COLUMN_ID+ " integer not null, "
      + QUANTITY + " varchar(3) not null," +
      " foreign key ("+ORDER_ID+") references "+TABLE_ORDER+"("+ORDER_ID+"),"+
      " foreign key ("+COLUMN_ID+") references "+TABLE_NAME+"("+COLUMN_ID+"), "+
      "primary key  ("+ORDER_ID+"," +COLUMN_ID+ "));";

这是我的EntryTO.java代码的一部分。

String orderid = orderid();
String orderdate = orderdate();
String notes = edtnotes.getText().toString();
String outletid = Globals.outletid;
order = dataSource.createorder(orderid,orderdate,notes, outletid);
        for(int i = 1; i< Globals.mylist.size(); i++){
        if(Globals.mylist.size()!=0){
            Globals.mylist.set(i,map1);
            map1.get(id);
            map1.get(qty);
            }
        orderdtl = dataSource.createorderdtl(orderid, Id, qty);

这是我的DBDataSource.java

public order createorder(String orderid,String orderdate, String notes, long outletid) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.ORDER_ID, orderid); 
    values.put(DBHelper.ORDER_DATE, orderdate); 
    values.put(DBHelper.NOTES, notes); 
    values.put(DBHelper.OUTLET_ID, outletid); 
    long insertId = database.insert(DBHelper.TABLE_ORDER, null, values);
    Cursor cursor = database.query(DBHelper.TABLE_ORDER,allorder,DBHelper.ORDER_ID +" = " + insertId, null,null, null, null);
    order neworder = null;
    if (cursor.moveToFirst()) {
        neworder = cursorToorder(cursor);
    }
    cursor.close();
    return neworder;} private order cursorToorder(Cursor cursor) {
    order order = new order();
    order.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));
    order.setorderdate(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_DATE)));
    order.setnotes(cursor.getString(cursor.getColumnIndex(DBHelper.NOTES)));
    order.setoutletid(cursor.getLong(cursor.getColumnIndex(DBHelper.OUTLET_ID)));
    return order;
    }
public orderdtl createorderdtl(String orderid, String Id, String qty) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.ORDER_ID, orderid);
    values.put(DBHelper.COLUMN_ID, Id);
    values.put(DBHelper.QUANTITY, qty);

    long insertId = database.insert(DBHelper.ORDER_DTL, null,
        values);

    Cursor cursor = database.query(DBHelper.ORDER_DTL,
        allorderdtl, DBHelper.ORDER_ID + "=" + insertId, null,
        null, null, null);
    orderdtl neworderdtl = null;
    if (cursor.moveToFirst()) {
        neworderdtl = cursorToorderdtl(cursor);
    }
    cursor.close();
    return neworderdtl;
  }
private orderdtl cursorToorderdtl(Cursor cursor) {
    orderdtl orderdtl = new orderdtl();
    Log.v("info", "The getString "+cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));
    Log.v("info", "The setLatLng "+cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ID))+","
                                  +cursor.getString(cursor.getColumnIndex(DBHelper.QUANTITY)));
    orderdtl.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));
    orderdtl.setId(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
    orderdtl.setqty(cursor.getString(cursor.getColumnIndex(DBHelper.QUANTITY)));
    return orderdtl;
    }

此代码有问题吗?我找不到哪里......请帮助我..我太困了。谢谢你。

1 个答案:

答案 0 :(得分:0)

SQL3支持5种类型的数据类型:

NULL。该值为NULL值。

INTEGER。该值是有符号整数,存储为1,2,3,4,6或8个字节,具体取决于值的大小。

REAL。该值是浮点值,存储为8字节IEEE浮点数。

TEXT。该值是一个文本字符串,使用数据库编码(UTF-8,UTF-16BE或UTF-16LE)存储。

BLOB。该值是一团数据,完全按照输入的方式存储。

请更改您的创建数据库语法,如下所示:

private static final String db_createorder = "create table  if not exists "
      + TABLE_ORDER + "("
      + ORDER_ID +" integer primary key autoincrement, "
      + ORDER_DATE + " integer, "
      + NOTES + " text not null, "
      + OUTLET_ID + " integer not null, " +
      "foreign key ("+ OUTLET_ID +") references "+TABLE_OUTLET+"("+ OUTLET_ID +"));";

private static final String db_createorderdtl = "create table  if not exists "
      + ORDER_DTL + "("
      + ORDER_ID +" integer, "
      + COLUMN_ID+ " integer not null, "
      + QUANTITY + " **** not null," +
      " foreign key ("+ORDER_ID+") references "+TABLE_ORDER+"("+ORDER_ID+"),"+
      " foreign key ("+COLUMN_ID+") references "+TABLE_NAME+"("+COLUMN_ID+"), "+
      "primary key  ("+ORDER_ID+"," +COLUMN_ID+ "));";

* * 数量可以是整数或实数或文本。我不知道你的应用程序。所以请根据您的申请决定

注意:无需插入ORDER_ID,当数据库中有行插入时,它会自动递增。

检查这些链接以获取更多信息:

http://mobile.tutsplus.com/tutorials/android/android-sdk_database-dates/ http://www.sqlite.org/datatype3.html