-2

Androidの初心者です。データベースプログラムに行き詰まりました。すべてのプログラムを何度もチェックしましたが、解決策が見つかりませんでした。クリックすると、残念ながらアプリケーションが停止します。logcat に次のエラーが表示されます。

<`10-20 13:03:10.910: E/SQLiteLog(982): (1) near "fromFRIENDSwhereCITYNOT": syntax error
10-20 13:03:10.930: D/AndroidRuntime(982): Shutting down VM
10-20 13:03:10.930: W/dalvikvm(982): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-20 13:03:11.120: E/AndroidRuntime(982): FATAL EXCEPTION: main
10-20 13:03:11.120: E/AndroidRuntime(982): java.lang.IllegalStateException: Could not execute method of the activity
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.view.View$1.onClick(View.java:3633)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.view.View.performClick(View.java:4240)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.view.View$PerformClick.run(View.java:17721)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.os.Handler.handleCallback(Handler.java:730)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.os.Looper.loop(Looper.java:137)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-20 13:03:11.120: E/AndroidRuntime(982):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 13:03:11.120: E/AndroidRuntime(982):  at java.lang.reflect.Method.invoke(Method.java:525)
10-20 13:03:11.120: E/AndroidRuntime(982):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-20 13:03:11.120: E/AndroidRuntime(982):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 13:03:11.120: E/AndroidRuntime(982):  at dalvik.system.NativeStart.main(Native Method)
10-20 13:03:11.120: E/AndroidRuntime(982): Caused by: java.lang.reflect.InvocationTargetException
10-20 13:03:11.120: E/AndroidRuntime(982):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 13:03:11.120: E/AndroidRuntime(982):  at java.lang.reflect.Method.invoke(Method.java:525)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.view.View$1.onClick(View.java:3628)
10-20 13:03:11.120: E/AndroidRuntime(982):  ... 11 more
10-20 13:03:11.120: E/AndroidRuntime(982): Caused by: android.database.sqlite.SQLiteException: near "fromFRIENDSwhereCITYNOT": syntax error (code 1): , while compiling: select * fromFRIENDSwhereCITYNOT IN(?,?)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
10-20 13:03:11.120: E/AndroidRuntime(982):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
10-20 13:03:11.120: E/AndroidRuntime(982):  at com.example.database.DatabaseActivity.clicked(DatabaseActivity.java:65)
10-20 13:03:11.120: E/AndroidRuntime(982):  ... 14 more
`>

私の活動は……

     <`package com.example.database;

import com.example.database.Contractclass.Friends;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DatabaseActivity extends Activity {
    DatabaseHelper dbHelper;
    String[]names={"sum","chan","hop","nav"};
    String[] cities={"mpl","kpm","vignr","nrml"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_database);
        Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();


        dbHelper=new DatabaseHelper(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.database, menu);
        return true;
    }
    public void clicked(View v)
    {

        SQLiteDatabase db;

        switch (((RadioGroup)findViewById(R.id.radioGroup1)).getCheckedRadioButtonId()) {
        case R.id.radio0:
            db=dbHelper.getWritableDatabase();
            ContentValues cv=new ContentValues();
            db.beginTransaction();
            for(int i=0;i<names.length;i++)
            {
                cv.put(Friends.COLUMN_NAME1, names[i]);
                cv.put(Friends.COLUMN_NAME2, cities[i]);
                db.insert(Friends.TABLE_NAME, null, cv);
            }
            db.endTransaction();
            db.close();
            Toast.makeText(this, "1", Toast.LENGTH_LONG).show();

            break;
        case R.id.radio1:
            TextView data=(TextView)findViewById(R.id.textView1);
            data.setText("");
            db=dbHelper.getReadableDatabase();
            Toast.makeText(this, "12", Toast.LENGTH_LONG).show();
            String sql="select * from "+Friends.TABLE_NAME+" where "+Friends.COLUMN_NAME2+" NOT IN(?,?)";
            String[] removecities={"mpl","kpm"};
            Cursor selecteddata=db.rawQuery(sql, removecities);
            if(selecteddata.getCount()==0)
            {
                data.setText("your are stupid");
            }
            else
            {
                while (selecteddata.moveToNext()) {
                    data.append(selecteddata.getString(selecteddata.getColumnIndex(Friends.COLUMN_NAME1))+","+selecteddata.getString(selecteddata.getColumnIndex(Friends.COLUMN_NAME2))+"\n");



                }
            }
            break;
        case R.id.radio2:
            db=dbHelper.getWritableDatabase();
            db.delete(Friends.TABLE_NAME, null, null);
            db.close();
            break;

        case R.id.radio3:
            db=dbHelper.getWritableDatabase();
            ContentValues cv2=new ContentValues();
            for(int i=0;i<cities.length;i++)
            {
                cv2.put(Friends.COLUMN_NAME2, (cities[i]+i));
                db.update(Friends.TABLE_NAME, cv2, Friends.COLUMN_NAME2+"=?", new String[]{cities[i]});

            }
            db.close();
            break;
        }
    }

}
`>      

私の契約クラス....

<`package com.example.database;
import android.provider.BaseColumns;
public class Contractclass {
    public static final int DATABASE_VERSION=3;
    public static final String DATABASE_NAME="Database.db";
     public static abstract class Friends implements BaseColumns{
    public static final String TABLE_NAME="FRIENDS";
    public static final String COLUMN_NAME1="NAME";
    public static final String COLUMN_NAME2="CITY";
    }
}
`>     

私のデータベースヘルパークラスは......

<
`
{
package com.example.database;
import com.example.database.Contractclass.Friends;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
    public DatabaseHelper(Context context) {
        super(context,  Contractclass.DATABASE_NAME , null, Contractclass.DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }
//  public DatabaseHelper(DatabaseActivity databaseActivity) {
//      // TODO Auto-generated constructor stub
//      super(null, null, null, 0);
//  }
    public static final String CREATE_DB="CREATE TABLE"+Friends.TABLE_NAME+"("+Friends._ID+"INTEGER"+" PRIMARYKEY,"+Friends.COLUMN_NAME1+"TEXT,"+Friends.COLUMN_NAME2+"TEXT)";
    public static final String DROP_DB="DROP TABLE IF EXISTS"+Friends.TABLE_NAME;
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try{
            db.execSQL(CREATE_DB);
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }
            }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        // TODO Auto-generated method stub
        try{
            db.execSQL(DROP_DB);
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }

        onCreate(db);
        }


}
`>
4

2 に答える 2

1

エラー ログは、単語間にスペースを入れずにクエリ「fromFRIENDSwhereCITY」を渡していることを示しています。「From FRIENDS where CITY」でなければならないので、当然 sqlite は構文エラーを発生させます。

于 2013-10-21T03:27:45.313 に答える