在android中创建数据库时出错

时间:2014-11-06 17:39:21

标签: android database

在eclipse上运行此代码时出现以下错误。我正在将编辑文本的数据输入到数据库中。

这是我的activity.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

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

这是我的activity.java代码如下:

package com.example.dailyexpenses;
import android.support.v7.app.ActionBarActivity;
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 mytable(expenditures number(8,2)");
     db.close();
   }

public void insert(View v)
{
     String name=et1.getText().toString();

        et1.setText("");

        db.execSQL("insert into mytable values(expenditures)");

}

}

我的日志目录如下:

11-06 11:06:59.850: E/SQLiteLog(900): (1) near ")": syntax error
11-06 11:06:59.850: D/AndroidRuntime(900): Shutting down VM
11-06 11:06:59.860: W/dalvikvm(900): threadid=1: thread exiting with uncaught exception (group=0xb2ab2ba8)
11-06 11:06:59.970: E/AndroidRuntime(900): FATAL EXCEPTION: main
11-06 11:06:59.970: E/AndroidRuntime(900): Process: com.example.dailyexpenses, PID: 900
11-06 11:06:59.970: E/AndroidRuntime(900): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dailyexpenses/com.example.dailyexpenses.Entertheexpenses}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: create table if not exists mytable(amount number(8,2)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.os.Looper.loop(Looper.java:136)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread.main(ActivityThread.java:5017)
11-06 11:06:59.970: E/AndroidRuntime(900):  at java.lang.reflect.Method.invokeNative(Native Method)
11-06 11:06:59.970: E/AndroidRuntime(900):  at java.lang.reflect.Method.invoke(Method.java:515)
11-06 11:06:59.970: E/AndroidRuntime(900):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-06 11:06:59.970: E/AndroidRuntime(900):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-06 11:06:59.970: E/AndroidRuntime(900):  at dalvik.system.NativeStart.main(Native Method)
11-06 11:06:59.970: E/AndroidRuntime(900): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: create table if not exists mytable(amount number(8,2)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
11-06 11:06:59.970: E/AndroidRuntime(900):  at com.example.dailyexpenses.Entertheexpenses.onCreate(Entertheexpenses.java:31)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.Activity.performCreate(Activity.java:5231)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-06 11:06:59.970: E/AndroidRuntime(900):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-06 11:06:59.970: E/AndroidRuntime(900):  ... 11 more

1 个答案:

答案 0 :(得分:1)

根据Log cat。以下一行错过了&#34;)&#34;靠近双引号

     db.execSQL("create table if not exists mytable(expenditures number(8,2)");

关闭括号