hopeTo hopeTo - 26 days ago 6
Android Question

Android SQLiteException "no such table"while compiling: INSERT INTO table

The code is giving no such table exception. I read almost all questions
about this exception, did all what it was said. but still it doesn't work.
I couldn't find what the problem is. is there any one who can help me?

public class Butcegiris extends Activity {
EditText bakici, krediAraba, krediOgrenim, krediTatil, faturaElektrik;
EditText faturaSu, faturaInternet, aidat, kaskoSigorta;
Spinner yillar,aylar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.butcegiris);

bakici = (EditText) findViewById(R.id.editTextBakici);
krediAraba = (EditText) findViewById(R.id.editTextKrediAraba);
krediOgrenim = (EditText) findViewById(R.id.editTextOgrenimKredisi);
krediTatil = (EditText) findViewById(R.id.editTextTatilKredisi);
faturaElektrik = (EditText) findViewById(R.id.editTextFaturaElektrik);
faturaSu = (EditText) findViewById(R.id.editTextFaturaSu);
faturaInternet = (EditText) findViewById(R.id.editTextFaturaInternet);
aidat = (EditText) findViewById(R.id.editTextAidat);
kaskoSigorta = (EditText) findViewById(R.id.editTextKaskoSigorta);
yillar=(Spinner)findViewById(R.id.spinnerYillar);
aylar=(Spinner)findViewById(R.id.spinnerAylar);

Button ileri = (Button) findViewById(R.id.buttonIleri);
ileri.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

boolean didItWork=true;
try {
int bakicisql = Integer.valueOf(bakici.getText().toString());
int krediArabasql = Integer.valueOf(krediAraba.getText()
.toString());
int krediOgrenimsql = Integer.valueOf(krediOgrenim.getText()
.toString());
int krediTatilsql = Integer.valueOf(krediTatil.getText()
.toString());
int faturaElektriksql = Integer.valueOf(faturaElektrik
.getText().toString());
int faturaSusql = Integer
.valueOf(faturaSu.getText().toString());
int faturaInternetsql = Integer.valueOf(faturaInternet
.getText().toString());
int aidatsql = Integer.valueOf(aidat.getText().toString());
int kaskoSigortasql = Integer.valueOf(kaskoSigorta.getText()
.toString());
String yil=yillar.getSelectedItem().toString();
String ay= aylar.getSelectedItem().toString();


TemporaryDatabase entry = new TemporaryDatabase(Butcegiris.this);

entry.open();

entry.createEntryGiris(yil,ay,bakicisql, krediArabasql, krediOgrenimsql,
krediTatilsql, faturaElektriksql, faturaSusql,
faturaInternetsql, aidatsql, kaskoSigortasql);

entry.close();
Intent intent = new Intent(Butcegiris.this, Butcehesapla.class);
startActivity(intent);
} catch (NumberFormatException e) {
didItWork=false;
} finally {

if(didItWork){

Toast.makeText(Butcegiris.this,"Success", Toast.LENGTH_LONG).show();

}

}

}
});
}

protected void onPause() {

super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.butcegiris_xml, menu);
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.ikinciSayfa:

Intent intent = new Intent(Butcegiris.this, Butcehesapla.class);
startActivity(intent);
return true;

}
return false;
}
}


TemporaryDatabase class

public class TemporaryDatabase

{

public static final String TAG = DbHelperTemporary.class.getSimpleName();
public static final String DB_NAME = "butcedb.sql";
private static String DB_PATH = "/data/data/com.deitel.btc/assets/";
public static final int DB_VERSION = 2;
public static final String DB_TABLE = "harcamalar";

public static final String C_ID = BaseColumns._ID;// Special for id
public static final String C_YIL = "spinnerYillar";
public static final String C_AY = "spinnerAylar";
public static final String C_BAKICI = "editTextBakici";
public static final String C_KREDIARABA = "editTextKrediAraba";
public static final String C_KREDIOGRENIM = "editTextKrediOgrenim";
public static final String C_KREDITATIL = "editTextKrediTatil";
public static final String C_FATURAELEKTRIK = "editTextFaturaElektrik";
public static final String C_FATURASU = "editTextFaturaSu";
public static final String C_FATURAINTERNET = "editTextFaturaInternet";
public static final String C_AIDAT = "editTextAidat";
public static final String C_KASKOSIGORTA = "editTextKaskoSigorta";

public static final String C_DIGERTAKSITLER = "editTextDigerTaksitler";
public static final String C_DIGER = "editTextDiger";
public static final String C_MAASSELO = "editTextMaasSelo";
public static final String C_MAASHILAL = "editTextMaasHilal";
public static final String C_DIGERGELIRLER = "editTextDigerGelirler";
public static final String C_TOPLAMHARCAMA = "editTextToplamHarcama";
public static final String C_TOPLAMGELIR = "editTextToplamGelir";
public static final String C_ELDEKALAN = "editTextEldeKalan";

public static final String CREATE_TABLE="CREATE TABLE ıf not exists harcamalar (C_ID int primary key autoincrement," +
"C_YIL int,C_AY TEXT,C_BAKICI int,C_KREDIARABA int,C_KREDIOGRENIM int,C_KREDITATIL int," +
"C_FATURAELEKTRIK int,C_FATURASU int,C_FATURAINTERNET int,C_AIDAT int,C_KASKOSIGORTA int," +
"C_DIGERTAKSITLER int,C_DIGER int,C_MAASSELO int,C_MAASHILAL int,C_DIGERGELIRLER int," +
"C_TOPLAMHARCAMA int,C_TOPLAMGELIR int, C_ELDEKALAN int);";


private DbHelperTemporary dbHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelperTemporary extends SQLiteOpenHelper {


public DbHelperTemporary(Context context) {

super(context, DB_NAME, null, DB_VERSION);


}

@Override
public void onCreate(SQLiteDatabase db) {

try {

Log.d(TAG, "onCreate sql: " + CREATE_TABLE);
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {

e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("drop table if exists " + DB_TABLE);
Log.w(TAG, "onUpdate drop table " + DB_TABLE);
onCreate(db);
}

}

public TemporaryDatabase(Context c) {
ourContext = c;

}
public void openDatabase() throws SQLException {
ourDatabase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
}

public void open() throws NumberFormatException {

try {
dbHelper = new DbHelperTemporary(ourContext);
ourDatabase = dbHelper.getWritableDatabase();
return ;
} catch (Exception e) {
Log.d(TAG,"Couldn't open database"+DB_NAME);
e.printStackTrace();
}
}

public void close() {

dbHelper.close();
ourDatabase.close();
}

public long createEntryGiris(String yilsql, String aysql, int bakicisql,
int krediArabasql, int krediOgrenimsql, int krediTatilsql,
int faturaElektriksql, int faturaSusql, int faturaInternetsql,
int aidatsql, int kaskoSigortasql) {

ContentValues cv = new ContentValues();
cv.put(C_YIL, yilsql);
cv.put(C_AY, aysql);
cv.put(C_BAKICI, bakicisql);
cv.put(C_KREDIARABA, krediArabasql);
cv.put(C_KREDIOGRENIM, krediOgrenimsql);
cv.put(C_KREDITATIL, krediTatilsql);
cv.put(C_FATURAELEKTRIK, faturaElektriksql);
cv.put(C_FATURASU, faturaSusql);
cv.put(C_FATURAINTERNET, faturaInternetsql);
cv.put(C_AIDAT, aidatsql);
cv.put(C_KASKOSIGORTA, kaskoSigortasql);

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

public String[] getData() {

String[] columns = new String[] { C_YIL, C_AY, C_BAKICI, C_KREDIARABA,
C_KREDIOGRENIM, C_KREDITATIL, C_FATURAELEKTRIK, C_FATURASU,
C_FATURAINTERNET, C_AIDAT, C_KASKOSIGORTA };
Cursor c = ourDatabase.query(DB_TABLE, columns, null, null, null, null,
null);
String[] result = new String[10];

//int i_yil = c.getColumnIndex(C_YIL);
//int i_ay = c.getColumnIndex(C_AY);
int i_bakici = c.getColumnIndex(C_BAKICI);
int i_krediaraba = c.getColumnIndex(C_KREDIARABA);
int i_krediogrenim = c.getColumnIndex(C_KREDIOGRENIM);
int i_kreditatil = c.getColumnIndex(C_KREDITATIL);
int i_faturaelektrik = c.getColumnIndex(C_FATURAELEKTRIK);
int i_faturasu = c.getColumnIndex(C_FATURASU);
int i_faturainternet = c.getColumnIndex(C_FATURAINTERNET);
int i_aidat = c.getColumnIndex(C_AIDAT);
int i_kaskosigorta = c.getColumnIndex(C_KASKOSIGORTA);

if (c.moveToLast()) {
result[0]=c.getString(i_bakici);
result[1]=c.getString(i_krediaraba);
result[2]=c.getString(i_krediogrenim);
result[3]=c.getString(i_kreditatil);
result[4]=c.getString(i_faturaelektrik);
result[5]=c.getString(i_faturasu);
result[6]=c.getString(i_faturainternet);
result[7]=c.getString(i_aidat);
result[8]=c.getString(i_kaskosigorta);


/*result = c.getString(i_bakici) + " " + c.getString(i_krediaraba)
+ " " + c.getString(i_krediogrenim) + " "+ c.getString(i_kreditatil) + " "+ c.getString(i_faturaelektrik) + " "
+ c.getString(i_faturasu) + " "+ c.getString(i_faturainternet) + " "
+ c.getString(i_aidat) + " " + c.getString(i_kaskosigorta);*/
}

return result;
}

}


And here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textButtonBakici"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textBakici"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/editTextBakici"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/altiyuzyirmibes"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textKrediAraba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textKrediAraba"
android:textAppearance="?android:attr/textAppearanceSmall" />


<EditText
android:id="@+id/editTextKrediAraba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/besyuzdoksan"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textOgrenimKredisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textOgrenimKredisi"
android:textAppearance="?android:attr/textAppearanceSmall" />


<EditText
android:id="@+id/editTextOgrenimKredisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/dortyuzon"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textTatilKredisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textTatilKredisi"
android:textAppearance="?android:attr/textAppearanceSmall" />



<EditText
android:id="@+id/editTextTatilKredisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/yuzotuzbes"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textFaturaElektrik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textFaturaElektrik"
android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
android:id="@+id/editTextFaturaElektrik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/sifir"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textFaturaSu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textFaturaSu"
android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
android:id="@+id/editTextFaturaSu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/sifir"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textFaturaInternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textFaturaInternet"
android:textAppearance="?android:attr/textAppearanceSmall" />


<EditText
android:id="@+id/editTextFaturaInternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/otuz"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textAidat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textAidat"
android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
android:id="@+id/editTextAidat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:hint="@string/sifir"
android:inputType="numberDecimal" />

</TableRow>

<TableRow
android:id="@+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textKaskoSigorta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textKaskoSigorta"
android:textAppearance="?android:attr/textAppearanceSmall" />



<EditText
android:id="@+id/editTextKaskoSigorta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="1"
android:ems="10"
android:hint="@string/ikiyuzyirmibes"
android:inputType="numberDecimal" >

<requestFocus />
</EditText>

</TableRow>
<TableRow
android:id="@+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10">

<Spinner
android:id="@+id/spinnerYillar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:entries="@array/Yillar"
android:prompt="@string/promptYillar" />

<Spinner
android:id="@+id/spinnerAylar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="3"
android:prompt="@string/prompt"
android:entries="@array/Aylar"
/>
</LinearLayout>

<Button
android:id="@+id/buttonIleri"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="0"
android:background="@drawable/navigation_forward" />

</TableRow>

</TableLayout>


I know it is long but I couldn't find the problem.
Thank you very much for your help.

no such table: harcamalar
Error inserting spinnerYillar=2012 editTextFaturaSu=9 editTextKrediOgrenim=9 editTextAidat=9 editTextFaturaElektrik=99 editTextKaskoSigorta=9 editTextKrediAraba=9 spinnerAylar=Ocak editTextFaturaInternet=6 editTextBakici=9 editTextKrediTatil=9

android.database.sqlite.SQLiteException: no such table: harcamalar (code 1): , while compiling: INSERT INTO harcamalar(spinnerYillar,editTextFaturaSu,editTextKrediOgrenim,editTextAidat,editTextFaturaElektrik,editTextKaskoSigorta,editTextKrediAraba,spinnerAylar,editTextFaturaInternet,editTextBakici,editTextKrediTatil) VALUES (?,?,?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.deitel.btc.TemporaryDatabase.createEntryGiris(TemporaryDatabase.java:131)
at com.deitel.btc.Butcegiris$1.onClick(Butcegiris.java:69)
at android.view.View.performClick(View.java:4202)
at android.view.View$PerformClick.run(View.java:17340)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-19 12:27:17.976: E/SQLiteDatabase(1023): at dalvik.system.NativeStart.main(Native Method)

Answer Source

Instead of int in the CREATE_TABLE statement, you should use integer

So use

C_ID integer primary key autoincrement

Check this doc

Quoted

you must use "INTEGER" not "INT". A column of type INT PRIMARY KEY is typeless just like any other

Instead of

CREATE TABLE ıf not exists harcamalar (C_ID int primary key autoincrement," +
            "C_YIL int,C_AY TEXT,C_BAKICI int .. so on

to

CREATE TABLE ıf not exists harcamalar (C_ID int primary key autoincrement," +
            C_YIL + "int," + C_AY + "TEXT,"+ C_BAKICI +" int .. so on

You need to append the value of variables rather than variable names itself