Android多数sql数据库错误

时间:2012-03-04 15:50:16

标签: android database sqlite

嗨朋友们。我正在尝试将一些数据存储到一个2表数据库中。这是它的定义:

public class SQLManager  {

public static String KEY_ROWID = "_id";
public static final String KEY_PRODUCT = "product";
public static final String KEY_QUANTITY = "quantity";
public static final String KEY_PRICE = "price";

public static final String KEY_DATA = "user data";

private static final String DATABASE_NAME = "PedidoDc";
private static final String DATABASE_TABLE1 = "PedidoTable";
private static final String DATABASE_TABLE2 = "DatosTable";

private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null,  DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE1 + " (" + 
                KEY_ROWID + " INTEGER PRIMARY KEY, " + 
                KEY_PRODUCT + " TEXT NOT NULL, " +
                KEY_QUANTITY + " TEXT NOT NULL, " +
                KEY_PRICE + " TEXT NOT NULL);"

        );

        db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + 
                KEY_ROWID + " INTEGER PRIMARY KEY, " + 
                KEY_DATA + " TEXT NOT NULL);"

        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
        onCreate(db);

    }


}

public SQLManager(Context c){

    ourContext = c;

}

public SQLManager open() throws SQLException{

    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

    public void close(){

    ourHelper.close();

}

     public long createEntry(String product, String quantity, String price) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_PRODUCT, product);
cv.put(KEY_QUANTITY, quantity);
cv.put(KEY_PRICE, price);

return ourDatabase.insert(DATABASE_TABLE1, null, cv);
  }

   public long createEntry2(String data) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_DATA, data);
return ourDatabase.insert(DATABASE_TABLE2, null, cv);
   }



    public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_PRODUCT, KEY_QUANTITY, KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE1, columns, null, null, null, null, null);
String result = "";

int iRow =  c.getColumnIndex(KEY_ROWID);
int iProduct =  c.getColumnIndex(KEY_PRODUCT);
int iQuantity =  c.getColumnIndex(KEY_QUANTITY);
int iPrice =  c.getColumnIndex(KEY_PRICE);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result = result  + " " + c.getString(iQuantity) + "    " + c.getString(iProduct) + "                        " + c.getString(iPrice) + "\n";
}

return result;
    }

    public String getUserData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_DATA};
Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);
String result = "";

int iRow =  c.getColumnIndex(KEY_ROWID);
int iData =  c.getColumnIndex(KEY_DATA);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result = result  + " " + c.getString(iData) + "\n";
}

return result;
    }
    public void deleteEntry(long l) {
// TODO Auto-generated method stub

ourDatabase.delete(DATABASE_TABLE1, null, null);


    }

     public String getQuantity(long l) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_QUANTITY, KEY_PRODUCT ,KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null){
    c.moveToFirst();
    String quantity = c.getString(1);
    c.close();
    return quantity;



}
return null;
    }

    public String getProduct(long l) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_QUANTITY, KEY_PRODUCT ,KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null){
    c.moveToFirst();
    String product = c.getString(2);
    c.close();
    return product;
}
return null;

    }

    public String getPrice(long l) {
// TODO Auto-generated method stub

String[] columns = new String[]{KEY_ROWID, KEY_QUANTITY, KEY_PRODUCT ,KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null){
    c.moveToFirst();
    String price = c.getString(3);
    c.close();
    return price;
}
return null;
    }


    }

这是我用来将一些数据存储到第二个表格的代码:

            SQLManager entry = new SQLManager(ConfirmaData.this);
            entry.open();
            entry.createEntry2(tlf);
            entry.close();

其中tlf是一个字符串变量。

我收到此错误:

E / Database(27790):android.database.sqlite.SQLiteException:near“data”:语法错误:,同时编译:INSERT INTO DatosTable(用户数据)VALUES(?);

有什么想法吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

我认为错误是由于包含空格的列名

public static final String KEY_DATA = "user data";

删除userdata

之间的空格

答案 1 :(得分:0)

我没有多少SQL经验,但您是否尝试过设置ID AUTOINCREMENT?既然你没有手动添加它?

答案 2 :(得分:0)

您没有在'createEntry2'方法中指定KEY_ROWID。

您可以将KEY_ROWID设为自动增量值

db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + 
    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    KEY_DATA + " TEXT NOT NULL);"
)