Chloe Chloe - 25 days ago 11
Android Question

Database is created but database table is not created

I have this set of coding that is used to insert the data that I've keyed in into database.
However, when I export the database, I don't see any tables

I wonder have I done it correctly.
No error was shown too.

logcat

01-21 07:55:25.567: E/SQLiteLog(772): (1) no such table: fuelLog
01-21 07:55:25.648: E/SQLiteDatabase(772): Error inserting tcost= 24 fuelprice=12 fcon= 61 odometer=123 date=12/12/12 fuelpump=2
01-21 07:55:25.648: E/SQLiteDatabase(772): android.database.sqlite.SQLiteException: no such table: fuelLog (code 1): , while compiling: INSERT INTO fuelLog(tcost,fuelprice,fcon,odometer,date,fuelpump) VALUES (?,?,?,?,?,?)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
01-21 07:55:25.648: E/SQLiteDatabase(772): at com.example.fuellog.DBAdapter.insertLog(DBAdapter.java:88)
01-21 07:55:25.648: E/SQLiteDatabase(772): at com.example.fuellog.MainActivity$4.onClick(MainActivity.java:156)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.view.View.performClick(View.java:4204)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.view.View$PerformClick.run(View.java:17355)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.os.Handler.handleCallback(Handler.java:725)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.os.Looper.loop(Looper.java:137)
01-21 07:55:25.648: E/SQLiteDatabase(772): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-21 07:55:25.648: E/SQLiteDatabase(772): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 07:55:25.648: E/SQLiteDatabase(772): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 07:55:25.648: E/SQLiteDatabase(772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 07:55:25.648: E/SQLiteDatabase(772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 07:55:25.648: E/SQLiteDatabase(772): at dalvik.system.NativeStart.main(Native Method)


This is my coding

public class DBAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_PRICE = "fuelprice";
public static final String KEY_FUEL = "fuelpump";
public static final String KEY_COST = "tcost";
public static final String KEY_ODM = "odometer";
public static final String KEY_CON = "fcon";

private static final String TAG = "DBADAPTER";

static final String DATABASE_NAME = "fuelLogDB";
static final int DATABASE_VERSION = 1;
static final String DATABASE_TABLE = "fuelLog";

private static final String DATABASE_CREATE =
"create table fuelLog (_id integer primary key auto increment, " +
"date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db)
{
try{
db.execSQL(DATABASE_CREATE);
}catch (SQLException e){
e.printStackTrace();
}
}//onCreate

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS fuelLog");
onCreate(db);

}
}

public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//close database

public void close()
{
DBHelper.close();
}

public long insertLog (String date, String fuelprice, String fuelpump, String tcost , String odometer,String fcon)

{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_PRICE, fuelprice);
initialValues.put(KEY_FUEL, fuelpump);
initialValues.put(KEY_COST, tcost);
initialValues.put(KEY_ODM, odometer);
initialValues.put(KEY_CON, fcon);
return db.insert(DATABASE_TABLE, null, initialValues);

}
}//DBAdapter


mainactivity.java

public class MainActivity extends Activity {

Button saveButton = null;
EditText dateEdit;
EditText priceEdit;
EditText pumpEdit;
TextView costView;
EditText odometerEdit;
TextView fconView;
TextWatcher textWatcher;
String priceEditStr ="",pumpEditStr="";
String odmEditStr = "";
int result;
int resultCon;



public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
odometerEdit = (EditText)findViewById(R.id.odometer);
fconView = (TextView)findViewById(R.id.fcon);


priceEdit.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();

if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}

}
});

pumpEdit.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();


if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}

}
});





odometerEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it

if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
odmEditStr = odometerEdit.getText().toString().trim();


if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
resultCon = Integer.parseInt(odmEditStr) / Integer.parseInt(pumpEditStr);
fconView.setText(" "+resultCon);
}

}
});


saveButton = (Button) findViewById(R.id.saveBTN);
saveButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
try
{
dbAdaptor.open();
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String cost = costView.getText().toString();
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
dbAdaptor.insertLog(date, price, pump, cost, odometer, fcon);

}
catch(Exception e){
Log.d("Fuel Log", e.getMessage());
}
finally
{
if(dbAdaptor != null)
dbAdaptor.close();
}
}
});

}//oncreate


}//main

Answer

In your DATABASE_CREATE string, it should be 'autoincrement', i.e. without any space or underscore in it.

Also, either uninstall your app completely before re-running it or change your database version to 2:

static final int DATABASE_VERSION = 2;

Because your code has already been through onCreate() in the SQLiteOpenHelper (you catch any errors that may occur in that method), I suspect it is not running. Doing the above should force it to do so again. Finally, remove the try/catch in your helper's onCreate(). Any failure to create the table will then crash your app at that point giving you the error you need to see in logcat.