0

私はAndroidプロジェクトにデータベースを実装するためにこのガイドに従ってきました。

これは私のDBAdapterクラスのコードです:

package com.sab.namespace;

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 DBAdapter
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_PROPERTYID = "propertyid";
    public static final String KEY_ADDRESS = "propertyaddress";
    public static final String KEY_JOBNO = "jobnumber";  
    public static final String KEY_ASSIGNED = "assignedto";
    public static final String KEY_COMPANY = "company";
    public static final String KEY_DATE = "datecreated";
    public static final String KEY_MASTERKEY = "usemasterkey"; 
    public static final String KEY_PHONEBEFORE = "phonebefore";
    public static final String KEY_PROBLEM = "descriptionofproblem";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "SABDatabase";
    private static final String DATABASE_TABLE = "jobs";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table jobs (_id integer primary key autoincrement, "
        + "propertyid text not null, propertyaddress text not null, "
        + "jobnumber text not null, assignedto text not null, "
        + "company text not null, datecreated text not null, "
        + "usemasterkey text not null, phonebefore text not null, "
        + "descriptionofproblem text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(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 jobs");
            onCreate(db);
        }
    } 

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

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


    //---insert a job into the database---
    public long insertJob(String propertyid, String propertyaddress, String jobnumber,
            String assignedto, String company, String datecreated, String usemasterkey,
            String phonebefore, String descriptionofproblem) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PROPERTYID, propertyid);
        initialValues.put(KEY_ADDRESS, propertyaddress);
        initialValues.put(KEY_JOBNO, jobnumber);
        initialValues.put(KEY_ASSIGNED, assignedto);
        initialValues.put(KEY_COMPANY, company);
        initialValues.put(KEY_DATE, datecreated);
        initialValues.put(KEY_MASTERKEY, usemasterkey);
        initialValues.put(KEY_PHONEBEFORE, phonebefore);
        initialValues.put(KEY_PROBLEM, descriptionofproblem);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

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

    //---retrieves all the jobs---
    public Cursor getAllJobs() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_PROPERTYID,
                KEY_ADDRESS,
                KEY_JOBNO,
                KEY_ASSIGNED,
                KEY_COMPANY,
                KEY_DATE,
                KEY_MASTERKEY,
                KEY_PHONEBEFORE,
                KEY_PROBLEM}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }


    //---retrieves a particular job---
    public Cursor getJob(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID, 
                        KEY_PROPERTYID,
                        KEY_ADDRESS,
                        KEY_JOBNO,
                        KEY_ASSIGNED,
                        KEY_COMPANY,
                        KEY_DATE,
                        KEY_MASTERKEY,
                        KEY_PHONEBEFORE,
                        KEY_PROBLEM}, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a job---
    public boolean updateJob(long rowId, String propertyid, String propertyaddress, String jobnumber,
            String assignedto, String company, String datecreated, String usemasterkey,
            String phonebefore, String descriptionofproblem)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_PROPERTYID, propertyid);
        args.put(KEY_ADDRESS, propertyaddress);
        args.put(KEY_JOBNO, jobnumber);
        args.put(KEY_ASSIGNED, assignedto);
        args.put(KEY_COMPANY, company);
        args.put(KEY_DATE, datecreated);
        args.put(KEY_MASTERKEY, usemasterkey);
        args.put(KEY_PHONEBEFORE, phonebefore);
        args.put(KEY_PROBLEM, descriptionofproblem);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}

次に、ViewJobs.javaクラスでデータベースを使用しようとします

DBAdapter db = new DBAdapter(this);
        db.open();        
        long id;
        id = db.insertJob(
                "PROP121",
                "MARK ANDREWS DRIVE",
                "JOB32",
                "COLIN",
                "SAB",
                "12/4/13",
                "yes",
                "yes",
                "SHIT IS EVERYWHERE");
        id = db.insertJob(
                "PROP122",
                "FAULTY CLOSE",
                "JOB33",
                "DAVE",
                "SAB",
                "13/4/13",
                "yes",
                "yes",
                "FIX PLEASE");
        db.close();
        Toast.makeText(this, Long.toString(id), Toast.LENGTH_LONG).show();

Toastは-1を出力します。これは、データの入力にエラーがあったことを示していますが、どこが間違っているのかわかりません。

エラーを無視して次のコードを続行すると、アプリケーションがクラッシュします。

db.open();
        Cursor c = db.getAllJobs();
        db.close();

誰かアドバイスをいただければ幸いです!

-ハリー

クラッシュしたときのlogcatコードは次のとおりです。

03-04 18:37:59.760: W/dalvikvm(2078): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-04 18:37:59.809: I/dalvikvm(2078): threadid=3: reacting to signal 3
03-04 18:37:59.958: E/AndroidRuntime(2078): FATAL EXCEPTION: main
03-04 18:37:59.958: E/AndroidRuntime(2078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sab.namespace/com.sab.namespace.ViewJobs}: android.database.sqlite.SQLiteException: no such table: jobs: , while compiling: SELECT _id, propertyid, propertyaddress, jobnumber, assignedto, company, datecreated, usemasterkey, phonebefore, descriptionofproblem FROM jobs
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.os.Looper.loop(Looper.java:137)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at dalvik.system.NativeStart.main(Native Method)
03-04 18:37:59.958: E/AndroidRuntime(2078): Caused by: android.database.sqlite.SQLiteException: no such table: jobs: , while compiling: SELECT _id, propertyid, propertyaddress, jobnumber, assignedto, company, datecreated, usemasterkey, phonebefore, descriptionofproblem FROM jobs
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at com.sab.namespace.DBAdapter.getAllJobs(DBAdapter.java:114)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at com.sab.namespace.ViewJobs.onCreate(ViewJobs.java:52)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.Activity.performCreate(Activity.java:4465)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-04 18:37:59.958: E/AndroidRuntime(2078):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-04 18:37:59.958: E/AndroidRuntime(2078):     ... 11 more
03-04 18:37:59.969: I/dalvikvm(2078): Wrote stack traces to '/data/anr/traces.txt'
03-04 18:38:00.328: I/dalvikvm(2078): threadid=3: reacting to signal 3
03-04 18:38:00.358: I/dalvikvm(2078): Wrote stack traces to '/data/anr/traces.txt'
03-04 18:38:00.658: I/dalvikvm(2078): threadid=3: reacting to signal 3
03-04 18:38:00.799: I/dalvikvm(2078): Wrote stack traces to '/data/anr/traces.txt'
03-04 18:38:03.210: I/Process(2078): Sending signal. PID: 2078 SIG: 9

これがViewJobs.javaの完全なコードです

package com.sab.namespace;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ViewJobs extends Activity {

    String titles[] = {"hello","bannana","frogshehe","hello","bannana","frogshehe","hello","bannana","frogshehe","hello","bannana","frogshehe","hello","bannana","frogshehe"};
    ListView vListView;
    ArrayAdapter<String> lVAAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);

        //database stuff
        DBAdapter db = new DBAdapter(this);
        db.open();        
        long id;
        id = db.insertJob(
                "PROP121",
                "MARK ANDREWS DRIVE",
                "JOB32",
                "COLIN",
                "SAB",
                "12/4/13",
                "yes",
                "yes",
                "SHIT IS EVERYWHERE");
        id = db.insertJob(
                "PROP122",
                "FAULTY CLOSE",
                "JOB33",
                "DAVE",
                "SAB",
                "13/4/13",
                "yes",
                "yes",
                "FIX PLEASE");
        db.close();
        Toast.makeText(this, Long.toString(id), Toast.LENGTH_LONG).show();

        db.open();
        Cursor c = db.getAllJobs();
        db.close();
        /*
        if (c.moveToFirst())
        {
            do {          
                DisplayJob(c);
            } while (c.moveToNext());
        }
        db.close();
        */

      //create list
        vListView = (ListView) findViewById(R.id.vlistview);
        lVAAdapter = new ArrayAdapter<String>(ViewJobs.this,
                android.R.layout.simple_list_item_1, titles);

        //create listener
        vListView.setAdapter(lVAAdapter);
        vListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> lVAAdapter, View myView,
                    int myItemInt, long mylng) {
                    String selectedFromList = (String) (vListView.getItemAtPosition(myItemInt));
                    if (titles[myItemInt] == "hello") {
                        titles[2] = "test";
                        vListView.invalidateViews();
                    }
            }
        });
    }

    public void DisplayJob(Cursor c)
    {
        Toast.makeText(this, 
                "id: " + c.getString(0) + "\n" +
                "propertyid: " + c.getString(1) + "\n" +
                "propertyaddress: " + c.getString(2) + "\n" +
                "jobnumber: " + c.getString(3) + "\n" +
                "assignedto: " + c.getString(4) + "\n" +
                "company: " + c.getString(5) + "\n" +
                "datecreated: " + c.getString(6) + "\n" +
                "usemasterkey: " + c.getString(7) + "\n" +
                "phonebefore: " + c.getString(8) + "\n" +
                "descriptionofproblem:  " + c.getString(9),
                Toast.LENGTH_LONG).show();        
    } 
}
4

1 に答える 1

0

おそらく、データベースを作成した後で、jobs テーブルを追加したでしょう。onCreate は呼び出されません。そのため、onCreate を再度呼び出すには、データをクリアする必要があります。

于 2013-03-04T18:56:29.257 に答える