Android sqlite应用程序无法启动并在打开应用程序后立即崩溃

时间:2013-04-15 22:43:15

标签: android database eclipse sqlite android-sqlite

我目前正在使用android adt在eclipse中工作,无法让我的应用程序打开。我已经尝试清理应用程序并多次卸载它,我不知道还能做些什么来让我的数据库显示信息。我在模拟器和我的motorola droid仿生Android设备上尝试这个。我是非常新的android编程所以任何类型的帮助将不胜感激,我的代码可以在下面找到。 eclipse软件的细节是:

Eclipse SDK 版本:4.2.1 构建ID:M20120914-1800

创建数据库的主要活动:

package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MainActivity 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_BUSINESS = "business";
    public static final String KEY_ADDRESS = "address";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_HOURS = "hours";
    public static final String KEY_WEB = "website";

    private static final String TAG = "MainActivity";

    private static final String DATABASE_NAME = "BloomBusiness";
    private static final String DATABASE_TABLE = "Business";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table Business (_id integer primary key autoincrement, "
        + "business text not null, address text not null, phone text not null, hours text not null" 
        + "website text not null)";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public MainActivity(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS Business");
            onCreate(db);
        }
    }    

    //---opens the database---
    public MainActivity open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertTitle(String business, String address, String phone, String hours, String website) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BUSINESS, business);
        initialValues.put(KEY_ADDRESS, address);
        initialValues.put(KEY_PHONE, phone);
        initialValues.put(KEY_HOURS, hours);
        initialValues.put(KEY_WEB, hours);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_BUSINESS,
                KEY_ADDRESS,
                KEY_PHONE,
                KEY_HOURS,
                KEY_WEB}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_BUSINESS, 
                        KEY_ADDRESS,
                        KEY_PHONE,
                        KEY_HOURS,
                        KEY_WEB
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String business, 
    String address, String phone, String hours, String website) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_BUSINESS, business);
        args.put(KEY_ADDRESS, address);
        args.put(KEY_PHONE, phone);
        args.put(KEY_HOURS,hours);
        args.put(KEY_WEB,website);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}

正在显示的数据库信息:

package com.example.database;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

public class DBUse extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity db = new MainActivity(this);


        db.open();
        Cursor c = db.getAllTitles();
        if(c.moveToFirst())
        {
            do{DisplayTitle(c);
        }while (c.moveToNext());
    }   
        db.open();
        Cursor b = db.getTitle(1);

        if (b.moveToFirst())
            DisplayTitle(c);
        else
            Toast.makeText(this,"No business found",Toast.LENGTH_LONG).show();
        db.close();
    db.close();


}

    public void DisplayTitle(Cursor c) {
        Toast.makeText(this,
        "Name: " + c.getString(1)+"\n"+
        "Address:" + c.getString(2)+"\n"+
        "Phone:" + c.getString(3)+"\n"+
        "Hours:" + c.getString(4)+"\n"+
        "Website"+ c.getShort(5),
        Toast.LENGTH_LONG).show();
    }


    }

正在添加数据库信息:

package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DBActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity db= new MainActivity(this);

        db.open();
        long id;

        id=db.insertTitle("BloomingFoods","419 E. Kirkwood Ave. Bloomington, IN", "812-336-5300", "M-S:8AM-9PM Sun:9am-8PM","http://www.bloomingfoods.coop");
        id=db.insertTitle("BloomingFoods","316 W. 6th Street Bloomington, IN", "812-333-5300", "M-S:7AM-10PM Sun:9am-9PM","http://www.bloomingfoods.coop");
        id=db.insertTitle("BloomingFoods","3220 E. 3rd Street Bloomington, IN", "812-336-5300", "M-Sun:8AM-10PM","http://www.bloomingfoods.coop");
        id=db.insertTitle("The Uptown Cafe","102 E. Kirkwood Ave. Bloomington, IN", "812-339-0900", "M-S:8AM-9PM Sun:9am-8PM", "http://www.the-uptown.com/");



    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.database"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.database.DBUse"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

logcat的:

04-15 20:50:23.122: E/Trace(638): error opening trace file: No such file or directory (2)
04-15 20:50:24.062: E/SQLiteLog(638): (1) no such column: website
04-15 20:50:24.072: D/AndroidRuntime(638): Shutting down VM
04-15 20:50:24.072: W/dalvikvm(638): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-15 20:50:24.084: E/AndroidRuntime(638): FATAL EXCEPTION: main
04-15 20:50:24.084: E/AndroidRuntime(638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.DBUse}: android.database.sqlite.SQLiteException: no such column: website (code 1): , while compiling: SELECT _id, business, address, phone, hours, website FROM Business
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.os.Looper.loop(Looper.java:137)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-15 20:50:24.084: E/AndroidRuntime(638):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 20:50:24.084: E/AndroidRuntime(638):  at java.lang.reflect.Method.invoke(Method.java:511)
04-15 20:50:24.084: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-15 20:50:24.084: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-15 20:50:24.084: E/AndroidRuntime(638):  at dalvik.system.NativeStart.main(Native Method)
04-15 20:50:24.084: E/AndroidRuntime(638): Caused by: android.database.sqlite.SQLiteException: no such column: website (code 1): , while compiling: SELECT _id, business, address, phone, hours, website FROM Business
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-15 20:50:24.084: E/AndroidRuntime(638):  at com.example.database.MainActivity.getAllTitles(MainActivity.java:101)
04-15 20:50:24.084: E/AndroidRuntime(638):  at com.example.database.DBUse.onCreate(DBUse.java:19)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.Activity.performCreate(Activity.java:5008)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-15 20:50:24.084: E/AndroidRuntime(638):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-15 20:50:24.084: E/AndroidRuntime(638):  ... 11 more

3 个答案:

答案 0 :(得分:1)

您尝试在“网站”列中选择包含数据的记录,但您的DATABASE CREATE不会添加此列。

Caused by: android.database.sqlite.SQLiteException: no such column: webiste (code 1): , while compiling: SELECT _id, business, address, phone, hours, webiste FROM Business

答案 1 :(得分:0)

null和phone之间需要逗号。

 private static final String DATABASE_CREATE =
    "create table Business (_id integer primary key autoincrement, "
    + "business text not null, address text not null, hours text not null," 
    + "phone text not null);";

答案 2 :(得分:0)

删除;从你的创建表字符串并尝试。在我的项目中,我没有使用;在create语句的末尾。

private static final String DATABASE_CREATE =
    "create table Business (_id integer primary key autoincrement, "
    + "business text not null, address text not null, hours text not null," 
    + "phone text not null)";

我删除了;在语句结束时就在null之后。