9

该代码没有给出这样的表异常。我阅读了几乎所有关于这个例外的问题,按照它所说的做了所有的事情。但它仍然不起作用。我找不到问题所在。有谁能帮助我吗?

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

临时数据库类

    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)
4

3 回答 3

5

您的 CREATE_TABLE 语句似乎不正确。

CREATE TABLE ıf

也许应该是

CREATE TABLE if

我之前在使用“如果不存在”表达式时遇到了问题,如果我的第一个建议无法尝试缩小范围,也许您可​​以尝试不使用它。您也可以尝试在 sql 语句中输入“integer”而不是“int”。不确定速记是否合法。

于 2012-12-19T13:15:59.450 回答
3

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

所以使用

C_ID integer primary key autoincrement

检查这个文档

您必须使用“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

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

于 2012-12-19T13:21:31.650 回答
1

我在构建我的应用程序时遇到了同样的问题,我尝试了一切,但没有任何效果。数据库与表名一起很好。

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

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

1-打开模拟器,转到设置 2-单击应用程序 3-滚动找到您的应用程序名称 4-双击它 5-强制停止 6-卸载它。

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

于 2017-09-26T22:10:13.123 回答