gpt4 book ai didi

java - 无法在android中创建数据库

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:41 25 4
gpt4 key购买 nike

我一直在尝试创建一个表格,并在单击插入按钮时通过表中的 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)

最佳答案

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

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

但是您必须像下面这样连接变量

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

关于java - 无法在android中创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26803179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com