Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid multitable sql database error
    primarykey
    data
    text
    <p><strong>Hi friends. I'm trying to store some data into a 2-table-database. Here is its definition:</strong></p> <pre><code>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; } } </code></pre> <p><strong>And this is the code i use to store some data to the second table:</strong></p> <pre><code> SQLManager entry = new SQLManager(ConfirmaData.this); entry.open(); entry.createEntry2(tlf); entry.close(); </code></pre> <p><strong>Where tlf is a string variable.</strong></p> <p>I get this error:</p> <p>E/Database(27790): android.database.sqlite.SQLiteException: near "data": syntax error: , while compiling: INSERT INTO DatosTable(user data) VALUES(?);</p> <p>Any idea?</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload