开发者

Connecting database in Android

开发者 https://www.devze.com 2023-03-01 01:38 出处:网络
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.

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
  开发者_如何学Python     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("");

   }
}


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

Connecting database in Android


(source: droidnova.com)

0

精彩评论

暂无评论...
验证码 换一张
取 消