jeudi 1 janvier 2015

Sending String values from EditText fields to SQLite Transaction Insert Error

Please help me with this issue, this is my first huge undertaking with Android and I've been busting my brain on this for a few hours now..


Short story is I am trying to set variables to the string values from EditText fields, then pass those to a transactional sql insert statement. However, I keep getting 12 logcat entries for "Explicit termination method 'close' not called". Since I have 12 objects (6 EditText and 6 strings), I'm assuming it's telling me that they arent being destroyed. However I suspect that it's because the environment is crashing because of some other reason (presumably because of some error I've made with my sql statement). I've been searching for hours and I jsut cant crack this.. Please help me!


This is the onClick event for my save button



@Override
public void onClick(View v) {

Context context = getActivity();
DBsql db = new DBsql(context);
SQLiteDatabase cptkdb = db.getWritableDatabase();

final EditText barcode = (EditText) v.findViewById(R.id.qbarcode);
final EditText name = (EditText) v.findViewById(R.id.qname);
final EditText product = (EditText) v.findViewById(R.id.qproduct);
final EditText quantity = (EditText) v.findViewById(R.id.qquantity);
final EditText expiration = (EditText) v.findViewById(R.id.qexpire);
final EditText note = (EditText) v.findViewById(R.id.qnote);

final String DATABASE_TABLE = "COUPONS";
final String INSERT_BARCODE = barcode.getText().toString();
final String INSERT_NAME = name.getText().toString();
final String INSERT_PRODUCT = product.getText().toString();
final Integer INSERT_QUANTITY = 1;
final String INSERT_EXPIRATION = expiration.getText().toString();
final String INSERT_NOTES = note.getText().toString();

final String prepstmt =
"INSERT INTO " + DATABASE_TABLE
+ " ( _id, QBARCODE, QNAME, QPRODUCT, QQUANTITY, QEXPIRATION, QCOMMENT ) " +
"VALUES ( ?, ?, ?, ?, ?, ? )";

cptkdb.beginTransactionNonExclusive();

SQLiteStatement stmt = cptkdb.compileStatement(prepstmt);

stmt.bindNull(1);
stmt.bindString(2, INSERT_BARCODE);
stmt.bindString(3, INSERT_NAME);
stmt.bindString(4, INSERT_PRODUCT);
stmt.bindLong(5, INSERT_QUANTITY);
stmt.bindString(6, INSERT_EXPIRATION);
stmt.bindString(7, INSERT_NOTES);

stmt.execute();
stmt.clearBindings();

cptkdb.setTransactionSuccessful();
cptkdb.endTransaction();

stmt.close();
cptkdb.close();
db.close();
}


This is my db configuration



public class DBsql extends SQLiteOpenHelper {

private static final String DB_NAME = "cptk.db";
private static final int DB_VERSION = 1;
Context dbcontext;

public static final String TABLE_COUPONS = "COUPONS";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_QBARCODE = "QBARCODE";
public static final String COLUMN_QNAME = "QNAME";
public static final String COLUMN_QPRODUCT = "QPRODUCT";
public static final String COLUMN_QQUANTITY = "QQUANTITY";
public static final String COLUMN_QEXPIRATION = "QEXPIRATION";
public static final String COLUMN_QCOMMENT = "QCOMMENT";


public DBsql(Context context) {

super(context, DB_NAME, null, DB_VERSION);
dbcontext = context;
}

// Parameter string to pass in order to create database
private static final String DB_CREATE = "CREATE TABLE "
+ TABLE_COUPONS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_QBARCODE + " VARCHAR NOT NULL, "
+ COLUMN_QNAME + " VARCHAR NOT NULL, "
+ COLUMN_QPRODUCT + " VARCHAR NOT NULL, "
+ COLUMN_QQUANTITY + " INTEGER NOT NULL, "
+ COLUMN_QEXPIRATION + " VARCHAR "
+ COLUMN_QCOMMENT + " VARCHAR"
+ ")";

@Override
public void onCreate(SQLiteDatabase database) {

database.execSQL(DB_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v(DBsql.class.getName(),
"Upgrading database from version "
+ oldVersion + " to "
+ newVersion + ", destroying old data!");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COUPONS);
onCreate(db);
}
}

Aucun commentaire:

Enregistrer un commentaire