Code for SQL class as follows:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class sqlDatabase {
public static final String KEY_ROWID = "_id";
public static final String KEY_PURCHASEREFUND = "_purchaserefund";
public static final String KEY_LOCATION = "_location";
public static final String KEY_SHOPTYPE = "_shoptype";
public static final String KEY_PRICE = "_price";
public static final String KEY_DATE = "_date";
public static final String KEY_CARDUSED = "_cardused";
private static final String DATABASE_NAME = "receiptdb";
private static final String DATABASE_TABLE = "receipttable";
private static final int DATABASE_VERSION = 1;
private receiptDBhelper receiptHelper;
private final Context receiptContext;
private SQLiteDatabase receiptDatabase;
private static class receiptDBhelper extends SQLiteOpenHelper{
public receiptDBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase rdb) {
// TODO Auto-generated method stub
rdb.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_CARDUSED + " TEXT NOT NULL, " +
KEY_LOCATION + " TEXT NOT NULL, " +
KEY_SHOPTYPE + " TEXT NOT NULL, " +
KEY_PRICE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase rdb, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
rdb.execSQL("DROP_TABLE_IF_EXISTS " + DATABASE_TABLE);
onCreate(rdb);
}
}
public sqlDatabase(Context c){
receiptContext = c;
}
public sqlDatabase open() throws SQLException {
receiptHelper = new receiptDBhelper(receiptContext);
receiptDatabase = receiptHelper.getWritableDatabase();
return this;
}
public void close(){
receiptHelper.close();
}
public long createEntry(String purchaserefund, String shoptype,
String location, String price, String cardused) {
// TODO Auto-generated method stub
ContentValues dbcv = new ContentValues();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
dbcv.put(KEY_PURCHASEREFUND, purchaserefund);
dbcv.put(KEY_SHOPTYPE, shoptype);
dbcv.put(KEY_LOCATION, location);
dbcv.put(KEY_PRICE, price);
dbcv.put(KEY_CARDUSED, cardused);
dbcv.put(KEY_DATE, dateFormat.format(date));
return receiptDatabase.insert(DATABASE_TABLE, null, dbcv);
}
}
Relevant code for class that uses the SQL class:
String purchaserefund = precspurchaserefunds;
String shoptype = precsshoptypes;
String location = precsshoptypesselection[precsshoptypeselection];
String price = precsprices;
String cardused = precscarddetails[precscardusedselection];
sqlDatabase entry = new sqlDatabase(precs.this);
entry.open();
entry.createEntry(purchaserefund, shoptype, location, price, cardused);
entry.close();
As far as I’m aware, all the code is there and is correct, but for some reason when I look into an SQL Database Browser, there are no values inserted?
I think the problem is actually how you are checking the sql db content. Usually the db file is stored under /data/package_name/databases (or something like that, I don’t remember the exact path), and without root access you cannot just read files inside that folder.
So with a SQL Database Browser that’s probably the issue..
Try to access your data through your app and see if the data is there. Also, keep an eye on LogCat, there might be some warnings/errors related to the Db.
(PS. there’s no need to call “commit”…)