在Android中连接数据库

时间:2011-04-28 08:55:09

标签: java android sqlite android-emulator database-connection

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

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("");

   }
}

1 个答案:

答案 0 :(得分:1)

您需要了解如何在Eclipse中进行调试以及如何使用ADBDDMS工具。

为了获得有关异常/强制关闭的更多详细信息,您需要在Eclipse中查找名为Logcat的视图(您将在DDMS透视图中找到),在那里您将找到详细的追溯何时/什么以及在什么方面是问题。

为此,您应该阅读有关Debugging in Android using Eclipse

的完整文章

alt text http://www.droidnova.com/blog/wp-content/uploads/2009/09/debugging-3.gif