编译时,Android SQLiteException“没有这样的表”:INSERT INTO表

时间:2012-12-19 13:04:16

标签: java android database eclipse sqlite

代码没有给出这样的表异常。我几乎读了所有问题 关于这个例外,做了所有的话。但它仍然不起作用。 我找不到问题所在。有没有人可以帮助我?

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类

    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;
    }

}

这是xml文件

    <?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>

我知道它很长但我找不到问题。 非常感谢你的帮助。

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)

2 个答案:

答案 0 :(得分:2)

而不是CREATE_TABLE语句中的int,您应该使用整数

所以使用

C_ID integer primary key autoincrement

选中此doc

引用

  

你必须使用“INTEGER”而不是“INT”。 INT PRIMARY KEY类型的列是   像任何其他

一样无类型

而不是

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

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

您需要附加变量的值而不是变量名称

答案 1 :(得分:0)

我在构建应用程序时遇到了同样的问题我尝试了所有内容并且没有任何效果数据库和表名一样好。

引起:android.database.sqlite.SQLiteException:没有这样的表:(代码1)Android

<强>解决方案: 解决问题的唯一方法是从模拟器中卸载应用程序:

1-打开模拟器,转到“设置” 2-点击应用程序 3-滚动查找您的应用程序名称 4-双击它 5-强制停止 6-取消安装它。

再次从android studio运行您的应用程序并尝试检查数据库连接。这对我有用,祝你好运。