0

当我启动我的应用程序时,我总是收到此消息:“不幸的是,'sql' 已停止”。这是我的 logcat 显示的内容:


    07-12 10:53:33.607: E/AndroidRuntime(940): FATAL EXCEPTION: main
    07-12 10:53:33.607: E/AndroidRuntime(940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.formation.sql/com.formation.sql.Main}: java.lang.IllegalArgumentException: column '_id' does not exist
    07-12 10:53:33.607: E/AndroidRuntime(940):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.os.Looper.loop(Looper.java:137)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at java.lang.reflect.Method.invoke(Method.java:511)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at dalvik.system.NativeStart.main(Native Method)
    07-12 10:53:33.607: E/AndroidRuntime(940): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.widget.CursorAdapter.init(CursorAdapter.java:168)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.widget.CursorAdapter.(CursorAdapter.java:116)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:52)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:78)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at com.formation.sql.Main.DataBind(Main.java:94)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at com.formation.sql.Main.onCreate(Main.java:33)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.Activity.performCreate(Activity.java:4465)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    07-12 10:53:33.607: E/AndroidRuntime(940):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    07-12 10:53:33.607: E/AndroidRuntime(940):  ... 11 more


这是主要的:


package com.formation.sql;

    import android.app.ListActivity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    import android.widget.Toast;


    public class Main extends ListActivity implements OnClickListener {
    /** Called when the activity is first created. */

    DBAdapter db;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getListView().setOnCreateContextMenuListener(this);
        ((Button)findViewById(android.R.id.button1)).setOnClickListener(this);
        db = new DBAdapter(this);
        db.open();
        DataBind();
    }

    // Création du menu principal
    public boolean onCreateOptionsMenu(Menu menu) {     
        menu.add(0,100,0,"Tout effacer");
        return true;
    }

     // Selection d'un item du menu
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case 100: 
            db.Truncate();
            DataBind();
            break;      
        }
        return true;
    }

    @Override // Selection d'un item de la liste
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Cursor cursor = (Cursor)l.getAdapter().getItem(position);
        String titre  = cursor.getString(cursor.getColumnIndex("titre"));
        Toast.makeText(this,"Item id "+id+" : "+titre, Toast.LENGTH_SHORT).show();
        super.onListItemClick(l, v, position, id);
    }

    @Override // Creation du menu contextuel
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)    {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Action");
        menu.add(0,100,0,"Supprimer");
        menu.add(0,200,1,"Editer");
    }

    @Override // Selection d'un item du menu contextuel
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info =   (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case 100:
            db.supprimerprovince(info.id);
            DataBind();
            break;
        case 200:
            Toast.makeText(this, "TODO", Toast.LENGTH_SHORT).show();                
            break;          
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        db.close();
        super.onDestroy();
    }


    public void DataBind(){
        Cursor c = db.recupererprovinceliste();
        startManagingCursor(c);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,   R.layout.list_item,c,new String[]{"titre","description","codebarre"}, new int[] {R.id.textTitre,R.id.TextDescription,R.id.TextCodeBarre});    setListAdapter(adapter);
    }

    public void onClick(View v) {
        long num = SystemClock.currentThreadTimeMillis();
        db.insererprovince(""+num);
        DataBind();
    }

}

这是创建数据库的 DBAdapter 代码:


    package com.formation.sql;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.Toast;


    public class DBAdapter {

    DatabaseHelper  DBHelper;
    Context         context;
    SQLiteDatabase  db;
    public DBAdapter(Context context){
        this.context = context;
        DBHelper = new DatabaseHelper(context);
    }   

    public class DatabaseHelper extends SQLiteOpenHelper{

        Context         context;

        public DatabaseHelper(Context context) {
            super(context, "provinces", null, 1);
            this.context = context;
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table if not exists provinces (_idprovince     INTEGER PRIMARY KEY AUTOINCREMENT,"
                    +"nomprovince text not null"
                    +");");         
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Toast.makeText(context, "Mise à jour de la Base de données version      "+oldVersion+" vers "+newVersion, Toast.LENGTH_SHORT).show();
            db.execSQL("DROP TABLE IF EXISTS provinces");
            onCreate(db);
        }

    }

    public DBAdapter open(){
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        db.close();
    }

    public void Truncate(){
        db.execSQL("DELETE FROM provinces");
    }

    public long insererprovince(String nom){
        ContentValues values = new ContentValues();
        values.put("nom", nom);
        return db.insert("provinces", null, values);
    }


       public boolean supprimerprovince(long id){

            return db.delete("provinces", "_idprovince="+id, null)>0;
        }

       public Cursor recupererprovinceliste(){

          return db.query("provinces",new String[]{"nomprovince"}, null, null, null, null, null);
        }
        /**
        * Getting all labels
        * returns list of labels
        * */
       public List getAllprovinces(){
           List labels = new ArrayList();

           // Select All Query
           String selectQuery = "SELECT  * FROM provinces"  ;

           SQLiteDatabase db = this.getReadableDatabase();
           Cursor c = db.rawQuery(selectQuery, null);

           // looping through all rows and adding to list
           if (c.moveToFirst()) {
               do {
                   labels.add(c.getString(1));
               } while (c.moveToNext());
           }

           // closing connection
           c.close();
           db.close();

           // returning lables
           return labels;
       }

        private SQLiteDatabase getReadableDatabase() {
            // TODO Auto-generated method stub
            return null;
        }
}

4

2 回答 2

1

查看导致异常的原因。

07-08 09:00:36.917: E/AndroidRuntime(825): Caused by:   
android.database.sqlite.SQLiteException: near ")":
syntax error: , while compiling: create table if not
exists provinces (_idprovince INTEGER PRIMARY KEY AUTOINCREMENT,
nomprovince text not null));

看起来你)最后可能有一个额外的。

于 2012-07-08T09:25:32.827 回答
0

在 db.executeSQL() 方法中有额外的右小括号。请在下面找到更正后的:

db.execSQL("create table if not exists provinces (_idprovince INTEGER PRIMARY KEY AUTOINCREMENT, "+"nomprovince text not null);");
于 2012-07-08T09:44:28.250 回答