-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 回答
0

错误的主要原因是,在第一次执行期间,即使活动停止,也会在内存中创建一个表(FRIENDS)。解决方案是将合同类中的数据库版本更改为比当前值更高的值。然后在执行 onUpgrade()方法被调用,新表(FRIENDS)将被创建并执行,而不会突然停止 Activity。

于 2013-10-29T15:25:44.560 回答