在Android中连接数据库

发布于 2024-11-04 10:22:18 字数 3117 浏览 0 评论 0原文

我正在尝试创建一个数据库并将值插入表中。但这段代码不起作用,任何人都可以帮助我。提前致谢。

package com.bookshop;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AutoCompleteTextView;
import android.database.sqlite.SQLiteDatabase;


public class BookShop extends Activity {
private AutoCompleteTextView txttitle;
private AutoCompleteTextView txtauthor;
private AutoCompleteTextView txtisbn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControls();     

    }


    private void initControls()
    {
        txttitle = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewtitle);
        txtauthor = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewauthor);
        txtisbn = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewisbn);
    }
   public void onsubmit(View view) {

       //txttitle.setText(ValueOf(txtauthor)+ ValueOf(txtisbn));

       // prepare the alert box
       AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

       // set the message to display
       alertbox.setMessage("Tittle : " + "" +(txttitle).getText()+"\n"+ "Author : "+"" + (txtauthor).getText()+"\n" + "ISBN : " +""+ (txtisbn).getText());

       // add a neutral button to the alert box and assign a click listener
       alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

           // click listener on the alert box
           public void onClick(DialogInterface arg0, int arg1) {
               // the button was clicked
               Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();

           }
       });

       // show it
       alertbox.show();      
       databaseacess();

   }

   public void databaseacess()
   {
       SQLiteDatabase db;

       db = openOrCreateDatabase(
            "BookShop.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );

       db.setVersion(1);
       db.setLocale(Locale.getDefault());
       db.setLockingEnabled(true);

       final String CREATE_TABLE_BookShop =
        "CREATE TABLE bookshop ("
        //+"id, INTEGER PRIMARY KEY AUTOINCREMENT"
        +"title TEXT"
        +"author TEXT" 
        +"isbn);";

       db.execSQL(CREATE_TABLE_BookShop);

       ContentValues values = new ContentValues();
       values.put("title",(txttitle).getText().toString());
       values.put("author",(txtauthor).getText().toString());
       values.put("isbn",(txtisbn).getText().toString());       

       try {
           db.insertOrThrow("bookshop", null, values );
       } catch (Exception e) {
           //catch code
       }

   }

   public void onclear(View view) {
      txttitle.setText("");
      txtauthor.setText("");
      txtisbn.setText("");

   }
}

I am trying to create a database and inserting values into table. but this code is not working can any one help me.thanks in advance.

package com.bookshop;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AutoCompleteTextView;
import android.database.sqlite.SQLiteDatabase;


public class BookShop extends Activity {
private AutoCompleteTextView txttitle;
private AutoCompleteTextView txtauthor;
private AutoCompleteTextView txtisbn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControls();     

    }


    private void initControls()
    {
        txttitle = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewtitle);
        txtauthor = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewauthor);
        txtisbn = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewisbn);
    }
   public void onsubmit(View view) {

       //txttitle.setText(ValueOf(txtauthor)+ ValueOf(txtisbn));

       // prepare the alert box
       AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

       // set the message to display
       alertbox.setMessage("Tittle : " + "" +(txttitle).getText()+"\n"+ "Author : "+"" + (txtauthor).getText()+"\n" + "ISBN : " +""+ (txtisbn).getText());

       // add a neutral button to the alert box and assign a click listener
       alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

           // click listener on the alert box
           public void onClick(DialogInterface arg0, int arg1) {
               // the button was clicked
               Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();

           }
       });

       // show it
       alertbox.show();      
       databaseacess();

   }

   public void databaseacess()
   {
       SQLiteDatabase db;

       db = openOrCreateDatabase(
            "BookShop.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );

       db.setVersion(1);
       db.setLocale(Locale.getDefault());
       db.setLockingEnabled(true);

       final String CREATE_TABLE_BookShop =
        "CREATE TABLE bookshop ("
        //+"id, INTEGER PRIMARY KEY AUTOINCREMENT"
        +"title TEXT"
        +"author TEXT" 
        +"isbn);";

       db.execSQL(CREATE_TABLE_BookShop);

       ContentValues values = new ContentValues();
       values.put("title",(txttitle).getText().toString());
       values.put("author",(txtauthor).getText().toString());
       values.put("isbn",(txtisbn).getText().toString());       

       try {
           db.insertOrThrow("bookshop", null, values );
       } catch (Exception e) {
           //catch code
       }

   }

   public void onclear(View view) {
      txttitle.setText("");
      txtauthor.setText("");
      txtisbn.setText("");

   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-11-11 10:22:18

您需要学习如何在 Eclipse 中调试以及如何使用 ADBDDMS 工具。

为了获得有关异常/强制关闭的更多详细信息,您需要在 Eclipse 中查找名为 Logcat(您将在 DDMS< /a> 透视图)在那里您将找到问题出现的时间/内容以及哪一行的详细回溯。

为此,您应该阅读关于 使用 Eclipse 在 Android 中进行调试的完整文章

替代文本
(来源:droidnova.com< /a>)

You need to learn how to Debug in Eclipse and how to use the ADB and DDMS tools.

In order to get more details about an exception/force close you need to look for a view in Eclipse called Logcat(you will find in the DDMS perspective) there you will find a detailed traceback when/what and on what line is the issue.

For this you should read a complete article about Debugging in Android using Eclipse

alt text
(source: droidnova.com)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文