无法在android中创建数据库

时间:2014-11-07 14:13:29

标签: java android

我一直在尝试创建一个表,并在单击按钮插入时通过表格中的editText插入一些数据。但是当我点击插入按钮时,应用程序停止工作。 这是我的activity.xml

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:text="enter here:">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter here"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="165dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Insert" />

</LinearLayout>

这是activity.java

    package com.example.dailyexpenses;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    import java.util.ArrayList;
    import android.view.View.OnClickListener;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    public class Entertheexpenses extends ActionBarActivity  {
   SQLiteDatabase db;
EditText et1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entertheexpenses);
    et1=(EditText)findViewById(R.id.editText1);
    db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
    db.execSQL("create table if not exists income(expenditures number(8,2))");
     Button but1= (Button)findViewById(R.id.button1);
       but1.setOnClickListener(new OnClickListener() 
     {
           public void onClick(View v) 
         {   
        String name=et1.getText().toString();
        double name1 = Double.parseDouble(name);
        db.execSQL("insert into income values(name1)");   
}
     });
}
}

这是logcat:

11-07 08:13:09.630: D/AndroidRuntime(1888): Shutting down VM
11-07 08:13:09.630: W/dalvikvm(1888): threadid=1: thread exiting with uncaught exception                   (group=0xb2a6cba8)
11-07 08:13:09.700: E/AndroidRuntime(1888): FATAL EXCEPTION: main
11-07 08:13:09.700: E/AndroidRuntime(1888): Process: com.example.dailyexpenses, PID: 1888
11-07 08:13:09.700: E/AndroidRuntime(1888): java.lang.IllegalStateException: attempt to re-open       an already-closed object: SQLiteDatabase: /data/data/com.example.dailyexpenses/databases/Mydb
11-07 08:13:09.700: E/AndroidRuntime(1888):     at     android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1657)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at com.example.dailyexpenses.Entertheexpenses$1.onClick(Entertheexpenses.java:52)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.view.View.performClick(View.java:4438)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.view.View$PerformClick.run(View.java:18422)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.os.Handler.handleCallback(Handler.java:733)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at android.os.Looper.loop(Looper.java:136)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at   android.app.ActivityThread.main(ActivityThread.java:5017)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at java.lang.reflect.Method.invoke(Method.java:515)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-07 08:13:09.700: E/AndroidRuntime(1888):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在执行查询之前检查数据库连接:

if (!dbHelper.db.isOpen())
  dbHelper.open();

希望这会有所帮助:)

答案 1 :(得分:0)

您的插入查询的问题是变量name1的值没有被提供给查询,您就是这样做的

  db.execSQL("insert into income values(name1)");   

但你必须对变量进行连接,如下面的

  db.execSQL("insert into income values("+name1+")");