Sqlite数据库表是不是在创建?

时间:2013-09-03 04:56:23

标签: android sqlite

我是android新手并使用SQLite数据库。我无法找到在数据文件夹内的DDMS中创建的数据库表。这是我的代码,我也无法在数据库中插入值而数据库表也没有创建。

public class BookingOpenHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "BookingDB";
    public static final String BOOKING_TABLE_NAME = "OnlineBooking";
    public static final String NAME= "name";
    public static final String QUANTITY = "quantity";
    public static final String NOTE = "note";
    public static final String NUMBER_OF_PIECES = "noofpieces";
    public static final String DATE_AND_TIME = "dateandtime";   
    public static final String DELIVARY_ADDRESS = "delivaryaddress";
    public static final int    VERSION = 3;
    public static final String CONTACT_NO = "contactno";    
    public static final String BOOKING_ID = "bookingid";    
    public static final String B_ID = "_id";



    private static final String CREATE_BOOKING_TABLE = "create table onlinebooking(_id integer not null primary key autoincrement,name text  null,contactnumber integer  null,quantity text  null,noofpcs integer null,dateandtime text null,delivaryaddress text null,note text null)";


    public BookingOpenHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db1) {
        // TODO Auto-generated method stub
        db1.execSQL(CREATE_BOOKING_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        onCreate(db1);
    }

}


Adapter.java


package com.example.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BookingAdapter {
    SQLiteDatabase database_ob;
    BookingOpenHelper openHelper_ob;
    Context context;

    public BookingAdapter(Context c) {// constructor
        context = c;
    }


    public BookingAdapter opnToRead() {// method for open and read the database to perform the operations
        openHelper_ob = new BookingOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getReadableDatabase();
        return this;
    }

    public BookingAdapter opnToWrite() {// method for open and write the database to perform the operations
        openHelper_ob = new BookingOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;
    }

    public void Close() {// method for closing the database
        database_ob.close();
    }

    public long insertDetails(String name, String contactnumber,String quantity,String noofpieces,String dateandtime,String delivaryaddress,String note) {// insert method for steps
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.NAME, name);
        contentValues.put(openHelper_ob.CONTACT_NO,contactnumber );
        contentValues.put(openHelper_ob.QUANTITY, quantity);        
        contentValues.put(openHelper_ob.NUMBER_OF_PIECES,noofpieces);
        contentValues.put(openHelper_ob.DATE_AND_TIME, dateandtime);
        contentValues.put(openHelper_ob.DELIVARY_ADDRESS,delivaryaddress);
        contentValues.put(openHelper_ob.NOTE,note);     
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.BOOKING_TABLE_NAME, null, contentValues);
        Close();
        return val;
    }

    public Cursor queryForSteps() {// method to display the edit profile name and its related steps.
        String[] cols = {openHelper_ob.BOOKING_ID, openHelper_ob.NAME, openHelper_ob.CONTACT_NO, openHelper_ob.QUANTITY,openHelper_ob.NUMBER_OF_PIECES,openHelper_ob.DATE_AND_TIME,openHelper_ob.DELIVARY_ADDRESS,openHelper_ob.NOTE };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.BOOKING_TABLE_NAME, cols,null, null, null, null, null);
        return c;
    }

    public Cursor queryAll() {// method to display the edit profile
        String[] cols = { openHelper_ob.BOOKING_ID, openHelper_ob.NAME, openHelper_ob.CONTACT_NO, openHelper_ob.QUANTITY,openHelper_ob.NUMBER_OF_PIECES,openHelper_ob.DATE_AND_TIME,openHelper_ob.DELIVARY_ADDRESS,openHelper_ob.NOTE  };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.BOOKING_TABLE_NAME, cols,null, null, null, null, null);
        return c;
    }


    public int getMaxID() {
        int id = 0;
        opnToWrite();
        final String MY_QUERY = "SELECT MAX(_id) AS _id FROM onlinebooking";
        Cursor cursor = database_ob.rawQuery(MY_QUERY, null);
        if (cursor.moveToFirst()) {
            id = cursor.getInt(0);
        }
        Close();
        return id;
    }

}

有些人可以帮助我解决这个问题。

09-03 05:01:42.880: E/AndroidRuntime(1016): FATAL EXCEPTION: main
09-03 05:01:42.880: E/AndroidRuntime(1016): java.lang.NullPointerException
090305:01:42.880:E/AndroidRuntime(1016):atcom.example.android.Online$1.onClick(Online.java:65)
09-03 05:01:42.880: E/AndroidRuntime(1016):     at android.view.View.performClick(View.java:4204)
09-03 05:01:42.880: E/AndroidRuntime(1016):     at android.view.View$PerformClick.run(View.java:17355)

online.java

package com.example.android;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;

public class Online extends FragmentActivity {
    private Spinner quantitySpinner;
    static EditText editDateTime;
    Button nextButton,cancelButton;
    EditText nameEdit,numberEdit,piecesEdit,delivaryEdit,noteEdit;
    BookingAdapter adapter;
    BookingOpenHelper helper_ob;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.online);

        nameEdit=(EditText) findViewById(R.id.nameEdit);
        numberEdit=(EditText) findViewById(R.id.contactNumber);

        quantitySpinner=(Spinner) findViewById(R.id.spinner);

        piecesEdit=(EditText) findViewById(R.id.editpieces);
        editDateTime=(EditText) findViewById(R.id.editdate);
        delivaryEdit=(EditText) findViewById(R.id.editaddress);
        noteEdit=(EditText) findViewById(R.id.editnote);
        nextButton=(Button) findViewById(R.id.nextformbtn);
        cancelButton=(Button) findViewById(R.id.cancelformbutton);


        nextButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String name=nameEdit.getText().toString();

                String contactnumber=numberEdit.getText().toString();

                String quantity = String.valueOf(quantitySpinner.getSelectedItem());

                String noofpieces=piecesEdit.getText().toString();

                String dateandtime=editDateTime.getText().toString();

                String delivaryaddress=delivaryEdit.getText().toString();

                String note=noteEdit.getText().toString();

                long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime, delivaryaddress, note);



            }
        });

        cancelButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Online.this,HomeScreen.class));
            }
        });

        quantitySpinner = (Spinner) findViewById(R.id.spinner);
        addListenerOnSpinnerItemSelection();
        editDateTime = (EditText) findViewById(R.id.editdate);
        editDateTime.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showTimePickerDialog(v);
                showDatePickerDialog(v);
            }
        });
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker arg0, int year, int month, int day) {
            // TODO Auto-generated method stub
            editDateTime.setText(day + "/" + (month + 1) + "/" + year);
        }
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // Do something with the time chosen by the user
            editDateTime.setText(editDateTime.getText() + " -" + hourOfDay + ":" + minute);
        }
    }

    private void addListenerOnSpinnerItemSelection() {
        quantitySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

    }
}

3 个答案:

答案 0 :(得分:1)

更改此行

  

private static final String CREATE_BOOKING_TABLE =“create table onlinebooking(_id integer not null主键autoincrement,name text null,contactnumber integer null,quantity text null,noofpcs integer null,dateandtime text null,delivaryaddress text null,note text null) “;

将上一行替换为此

  

private static final String CREATE_BOOKING_TABLE =“create table onlinebooking(_id integer not null主键autoincrement,name text null,contactnumber integer null,quantity text null,noofpcs integer null,dateandtime text null,delivaryaddress text null,note text null) ;“;

答案 1 :(得分:0)

long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime, delivaryaddress, note);

你的一个或多个变量返回一个空值,这就是问题的根源。尝试打印所有变量的值,此调试将导致您的错误。

答案 2 :(得分:0)

创建" adapter"的实例使用前

adapter=new BookingAdapter (Context);//write this line starting of oncreate method

long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime,   delivaryaddress, note);