0

我是 android 的新手,我的项目是添加一个产品和他的参考资料(nom、qte_alerte、prix_achat、prix_vente、description)日志猫说我认为这不是描述的笨拙。问题出在哪里。。我可以在哪里添加它,我真的需要这个应用程序。我需要在 logCat 中显示我添加的产品列表(产品 ajouté)

05-10 15:27:01.701: E/AndroidRuntime(1556): FATAL EXCEPTION: main
05-10 15:27:01.701: E/AndroidRuntime(1556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tabet.salah_projet/com.tabet.salah_projet.ChecklisteProduit}: android.database.sqlite.SQLiteException: no such column: description (code 1): , while compiling: SELECT id, nom_produit, prix_achat, prix_vente, description FROM produittable ORDER BY nom_produit asc
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.os.Looper.loop(Looper.java:137)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at dalvik.system.NativeStart.main(Native Method)
05-10 15:27:01.701: E/AndroidRuntime(1556): Caused by: android.database.sqlite.SQLiteException: no such column: description (code 1): , while compiling: SELECT id, nom_produit, prix_achat, prix_vente, description FROM produittable ORDER BY nom_produit asc
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at com.tabet.salah_projet.ProduitDataAdapter.selectAll(ProduitDataAdapter.java:46)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at com.tabet.salah_projet.ChecklisteProduit.onCreate(ChecklisteProduit.java:24)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.Activity.performCreate(Activity.java:5104)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-10 15:27:01.701: E/AndroidRuntime(1556):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-10 15:27:01.701: E/AndroidRuntime(1556):     ... 11 more

对于我的数据库,我有

package com.tabet.salah_projet;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.List;

public class ProduitDataAdapter {
    private static final  String DATABASE_NAME = "produitbase.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_PRODUIT = "produittable";
    private static Context context;
    static SQLiteDatabase db;
private SQLiteStatement insertStmt;

    private static final String INSERT = "insert into "
        + TABLE_PRODUIT + " (nom_produit,quntité_alerte,prix_achat,prix_vente,description_produit) values (?,?,?,?,?)";

    public ProduitDataAdapter(Context context) {
        ProduitDataAdapter.context = context;
        OpenHelper openHelper = new OpenHelper(ProduitDataAdapter.context);
        ProduitDataAdapter.db = openHelper.getWritableDatabase();
        this.insertStmt = ProduitDataAdapter.db.compileStatement(INSERT);
       }

    public long insert(String nom_produit,String quntité_alerte,String prix_achat,String prix_vente,String description_produit) {
        this.insertStmt.bindString(1, nom_produit);
        this.insertStmt.bindString(2, quntité_alerte);
        this.insertStmt.bindString(3, prix_achat);
        this.insertStmt.bindString(4, prix_vente);
        this.insertStmt.bindString(5, description_produit);
        return this.insertStmt.executeInsert();
    }

    public void deleteAll() {
        db.delete(TABLE_PRODUIT, null, null);
    }

    public List<String[]> selectAll()
    {

        List<String[]> list = new ArrayList<String[]>();
        Cursor cursor = db.query(TABLE_PRODUIT, new String[] { "id","nom_produit","prix_achat","prix_vente","description" },
                null, null, null, null, "nom_produit asc"); 

        int x=0;
        if (cursor.moveToFirst()) {
            do {
                String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};

                list.add(b1);

                x=x+1;
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        } 
        cursor.close();

        return list;
    }


    public void delete(int rowId) {
        db.delete(TABLE_PRODUIT, null, null); 
    }

    private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_PRODUIT + " (id INTEGER PRIMARY KEY, nom_produit TEXT NOT NULL, quntité_alerte TEXT NOT NULL, prix_achat TEXT NOT NULL, prix_vente TEXT NOT NULL, description_produit TEXT NOT NULL)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUIT);
            onCreate(db);
        }
    }


}

第一个活动

package com.tabet.salah_projet;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class ProduitActivity extends Activity implements OnClickListener{
    private ProduitDataAdapter dp;     
    static final int DIALOG_ID = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produits);
        View add = findViewById(R.id.btnAjout);
        add.setOnClickListener(this);
        View home = findViewById(R.id.btnAnnuler);
        home.setOnClickListener(this);
         View liste = findViewById(R.id.link_to_liste);
        liste.setOnClickListener(this);
        }
    public void onClick(View v){
        switch(v.getId()){

        case R.id.btnAnnuler:
        // @_@  Intent i = new Intent(this, AndroidDashboardDesignActivity.class);
        //@_@   startActivity(i);
            break;

        case R.id.link_to_liste:
            Intent ilis = new Intent(this, ChecklisteProduit.class);  
            startActivity(ilis);
                break;

        case R.id.btnAjout:
            View editText1 = (EditText) findViewById(R.id.Nom_produit);
            View editText2 = (EditText) findViewById(R.id.qte_alerte_produit);
            View editText3 = (EditText) findViewById(R.id.Prix_achat_produit);
            View editText4 = (EditText) findViewById(R.id.Prix_vente_produit);
            View editText5 = (EditText) findViewById(R.id.txtDescription);

            String myEditText1=((TextView) editText1).getText().toString();
            String myEditText2=((TextView) editText2).getText().toString();
            String myEditText3=((TextView) editText3).getText().toString();
            String myEditText4=((TextView) editText4).getText().toString();
            String myEditText5=((TextView) editText5).getText().toString();

            this.dp = new ProduitDataAdapter(this);
            this.dp.insert(myEditText1,myEditText2,myEditText3,myEditText4,myEditText5);

            showDialog(DIALOG_ID);
            break;

        }
    }  
    protected final Dialog onCreateDialog(final int id) {
        Dialog dialog = null;
        switch(id) {
        case DIALOG_ID:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Information saved successfully ! Add Another Info?")
            .setCancelable(false)
            .setPositiveButton("Non", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ProduitActivity.this.finish();

              }
            })
            .setNegativeButton("Oui", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create(); 
            dialog = alert;
            break;

        default:

        }
        return dialog;
    }



}

并且很好地列出产品的活动问题请帮助我,我开始失去希望

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ChecklisteProduit extends ListActivity  {
    TextView selection;
    public int idToModify; 
    ProduitDataAdapter dm;

    List<String[]> list = new ArrayList<String[]>();
    List<String[]> names2 =null ;
    String[] stg1;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cheklisteprod);
          dm = new ProduitDataAdapter(this);
          names2 = dm.selectAll();

        stg1=new String[names2.size()]; 

        int x=0;
        String stg;

        for (String[] name : names2) {
            stg = name[1]+" - "+name[2]+ " - "+name[3]+" - "+name[4]+" - "+name[5];

            stg1[x]=stg;
            x++;
        }


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
                this,android.R.layout.simple_list_item_1,   
                stg1);
        this.setListAdapter(adapter);
        selection=(TextView)findViewById(R.id.selection);

    }      

    public void onListItemClick(ListView parent, View v, int position, long id) {
        selection.setText(stg1[position]);
    }

}
4

1 回答 1

0

您的 sql 查询错误:

Caused by: android.database.sqlite.SQLiteException: no such column: description (code 1): , while compiling: SELECT id, nom_produit, prix_achat, prix_vente, description FROM produittable ORDER BY nom_produit asc

确保您在“produittable”表中有“描述”列

根据你的代码:

 @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_PRODUIT + " (id INTEGER PRIMARY KEY, nom_produit TEXT NOT NULL, quntité_alerte TEXT NOT NULL, prix_achat TEXT NOT NULL, prix_vente TEXT NOT NULL, description_produit TEXT NOT NULL)");
    }

您正在创建列“ description_produit

于 2013-05-10T15:46:37.047 回答