-1

Eclipse を使用して Android アプリケーションを作成しました。1 つのボタンを使用して情報を保存し、別のボタンを使用してデータベースから情報を取得しました。最初のものは機能していますが、VIEW ボタンを押してデータを表示すると、「残念ながら MyDatabase が停止しました」と表示されます。私はあなたにすべての情報を与えています。メインクラス SqlLiteExample.java :

public class SqlLiteExample extends Activity implements OnClickListener {
Button sqlUpdate, sqlView;
EditText etName,etAge,etContact;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlliteexample);
    sqlUpdate = (Button) findViewById(R.id.savebutton);
    etName = (EditText) findViewById(R.id.editName);
    etAge = (EditText) findViewById(R.id.editAge);
    etContact = (EditText) findViewById(R.id.editContact);

    sqlView = (Button) findViewById(R.id.viewbutton);
    sqlView.setOnClickListener(this);
    sqlUpdate.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.savebutton:
        boolean didWork = true;
        try{
        String name = etName.getText().toString();
        String age = etAge.getText().toString();
        String contact = etContact.getText().toString();

        MyDB entry = new MyDB(SqlLiteExample.this);
        entry.open();
        entry.createEntry(name,age,contact);
        entry.close();
        }catch(Exception e){
            didWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Error");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if(didWork){
                Dialog d = new Dialog(this);
                d.setTitle("Updated");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.viewbutton:
        Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS");
        startActivity(i);
        break;
    }
}

}

データベースを制御するには、 MyDB.java を使用しました:

public class MyDB {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_AGE = "persons_age";
public static final String KEY_CONTACT = "persons_contact";

private static final String DATABASE_NAME = "MyDatabase";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_AGE + "TEXT NOT NULL, " +
                KEY_CONTACT + "TEXT NOT NULL);"

                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }       
}
public MyDB(Context c){
    ourContext = c;
}
public MyDB open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}
public long createEntry(String name, String age, String contact) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_AGE, age);
    cv.put(KEY_CONTACT, contact);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_AGE, KEY_CONTACT};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iAGE = c.getColumnIndex(KEY_AGE);
    int iCONTACT = c.getColumnIndex(KEY_CONTACT);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iAGE) + " " + c.getString(iCONTACT) +"\n";
    }
    return result;
}

}

情報を表示する別のアクティビティは、DisplayRecords.java です。

public class DisplayRecords extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);
    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    MyDB info = new MyDB(this);

    try {
        info.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String data = info.getData();
    info.close();
    tv.setText(data);
}

}

メイン xml ファイルは sqlliteexample.xml です。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/age" >
        </TextView>

    <EditText
        android:id="@+id/editAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textContcat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/contact" />

    <EditText
        android:id="@+id/editContact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textMultiLine" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/savebutton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/save"/>"

    <Button
        android:id="@+id/viewbutton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/view" />

</LinearLayout>

表示する別のレイアウト… sqlview.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Names" >
        </TextView>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="@string/age" >
        </TextView>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="@string/contacts" >
        </TextView>
    </TableRow>
</TableLayout>

<TextView
    android:id="@+id/tvSQLinfo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.5"
    android:text="@string/gt" >
</TextView>

そしてmanifest.xmlは次のとおりです。

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="14" />
<compatible-screens></compatible-screens>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DisplayRecords" android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.bysakiralam.mydatabase.DISPLAYRECORDS" /> 
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

私の間違いはどこですか?助けてください。LogCat ファイルの内容:

07-11 16:46:11.312: E/Trace(668): error opening trace file: No such file or directory (2)
07-11 16:46:15.531: D/gralloc_goldfish(668): Emulator without GPU emulation detected.
07-11 16:46:32.695: I/Choreographer(668): Skipped 47 frames!  The application may be doing too much work on its main thread.
07-11 12:26:37.454: I/Choreographer(668): Skipped 37 frames!  The application may be doing too much work on its main thread.
07-11 12:26:40.614: I/Choreographer(668): Skipped 31 frames!  The application may be doing too much work on its main thread.
07-11 12:26:43.694: D/dalvikvm(668): GC_CONCURRENT freed 81K, 8% free 2688K/2892K, paused 73ms+10ms, total 235ms
07-11 12:26:46.934: E/SQLiteLog(668): (1) table peopleTable has no column named persons_contact
07-11 12:26:46.984: E/SQLiteDatabase(668): Error inserting persons_contact=wert persons_age=32 persons_name=sad
07-11 12:26:46.984: E/SQLiteDatabase(668): android.database.sqlite.SQLiteException: table peopleTable has no column named persons_contact (code 1): , while compiling: INSERT INTO peopleTable(persons_contact,persons_age,persons_name) VALUES (?,?,?)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at com.bysakiralam.mydatabase.MyDB.createEntry(MyDB.java:68)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at com.bysakiralam.mydatabase.SqlLiteExample.onClick(SqlLiteExample.java:46)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.view.View.performClick(View.java:4204)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.view.View$PerformClick.run(View.java:17355)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.os.Handler.handleCallback(Handler.java:725)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.os.Looper.loop(Looper.java:137)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-11 12:26:46.984: E/SQLiteDatabase(668):  at dalvik.system.NativeStart.main(Native Method)
07-11 12:26:54.745: I/Choreographer(668): Skipped 55 frames!  The application may be doing too much work on its main thread.
07-11 12:26:56.694: E/SQLiteLog(668): (1) no such column: persons_age
07-11 12:26:56.745: D/AndroidRuntime(668): Shutting down VM
07-11 12:26:56.745: W/dalvikvm(668): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-11 12:26:56.934: E/AndroidRuntime(668): FATAL EXCEPTION: main
07-11 12:26:56.934: E/AndroidRuntime(668): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bysakiralam.mydatabase/com.bysakiralam.mydatabase.DisplayRecords}: android.database.sqlite.SQLiteException: no such column: persons_age (code 1): , while compiling: SELECT _id, persons_name, persons_age, persons_contact FROM peopleTable
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.os.Looper.loop(Looper.java:137)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-11 12:26:56.934: E/AndroidRuntime(668):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:26:56.934: E/AndroidRuntime(668):  at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:26:56.934: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 12:26:56.934: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-11 12:26:56.934: E/AndroidRuntime(668):  at dalvik.system.NativeStart.main(Native Method)
07-11 12:26:56.934: E/AndroidRuntime(668): Caused by: android.database.sqlite.SQLiteException: no such column: persons_age (code 1): , while compiling: SELECT _id, persons_name, persons_age, persons_contact FROM peopleTable
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
07-11 12:26:56.934: E/AndroidRuntime(668):  at com.bysakiralam.mydatabase.MyDB.getData(MyDB.java:73)
07-11 12:26:56.934: E/AndroidRuntime(668):  at com.bysakiralam.mydatabase.DisplayRecords.onCreate(DisplayRecords.java:25)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.Activity.performCreate(Activity.java:5104)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-11 12:26:56.934: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-11 12:26:56.934: E/AndroidRuntime(668):  ... 11 more
07-11 12:27:01.954: I/Process(668): Sending signal. PID: 668 SIG: 9
4

3 に答える 3

1

アプリケーションを実行するときにログ cat を確認する必要があります。エラーが発生した行と場所が通知されます。これにより、何が問題なのかがよくわかります。

于 2013-07-11T12:08:03.283 に答える
0

変化 :

 public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT NOT NULL, " +
            KEY_AGE + "TEXT NOT NULL, " +
            KEY_CONTACT + "TEXT NOT NULL);"

            );

     public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT NOT NULL, " +
            KEY_AGE + " TEXT NOT NULL, " +
            KEY_CONTACT + " TEXT NOT NULL);"

            );

理由: 以下にスペースを入れていませんでした:

  KEY_AGE + " TEXT NOT NULL, " +  KEY_CONTACT + " TEXT NOT NULL);"
于 2013-07-11T12:20:36.260 に答える
-1

変化する

   db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT NOT NULL, " +
            KEY_AGE + " TEXT NOT NULL, " +
            **KEY_CONTACT + " TEXT NOT NULL);"**

            );

    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT NOT NULL, " +
            KEY_AGE + " TEXT NOT NULL, " +
            **KEY_CONTACT + " TEXT NOT NULL)"
            );**

小さな構文エラーがありました

于 2013-07-11T12:37:19.810 に答える