SQLite表不存在

时间:2013-06-15 06:53:34

标签: android sqlite

我在从数据库接收数据时遇到问题。根据我的代码确定,我的数据库(TBL_MAHRIE)包含三列(ID,COL_SAL,COL_NERKH)。运行我的项目,我收到了这些错误:

06-15 06:37:01.877: E/SQLiteLog(850): (1) no such table: TBL_MAHRIE
06-15 06:37:01.987: E/SQLiteDatabase(850): Error inserting COL_SAL=1392 COL_NERKH=250
06-15 06:37:01.987: E/SQLiteDatabase(850): android.database.sqlite.SQLiteException: nosuch table: TBL_MAHRIE (code 1): , while compiling: INSERT INTO TBL_MAHRIE    (COL_SAL,COL_NERKH) VALUES (?,?)
06-15 06:37:01.987: E/SQLiteDatabase(850):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

调试我的代码,我发现我的问题出在getAllShakhes()类。

我的课程是:

DataBaseHandler.java

package ir.dadpardaz.mahrie_dadpardaz;

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ToggleButton;

public class DataBaseHandler {

SQLiteDatabase database;

public DataBaseHandler(Context context) {
    ShakhesDatabaseOpenHelper shakhesDatabaseOpenHelper = new ShakhesDatabaseOpenHelper(
            context, "shakhsdb.db", null, 1);
    database = shakhesDatabaseOpenHelper.getWritableDatabase();
}

public class ShakhesDatabaseOpenHelper extends SQLiteOpenHelper {

    public ShakhesDatabaseOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "create table TBL_MAHRIE (ID INTEGER PRIMARY KEY, COL_SAL TEXT, COL_NERKH TEXT)";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

}

public long insertShakhes(Shakhes shakhes) {
    ContentValues values = new ContentValues();
    values.put("COL_SAL", shakhes.getSal());
    values.put("COL_NERKH", shakhes.getNerkh());
    return database.insert("TBL_MAHRIE", null, values);
}

boolean isTableExists(SQLiteDatabase db, String tableName) {
    if (tableName == null || db == null || !db.isOpen()) {
        return false;
    }
    Cursor cursor = db
            .rawQuery(
                    "SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?",
                    new String[] { "table", tableName });
    if (!cursor.moveToFirst()) {
        return false;
    }
    int count = cursor.getInt(0);
    cursor.close();
    return count > 0;
}

public List<Shakhes> getAllShakhes() {

    List<Shakhes> shakhes = null;

    Cursor c = database.query("TBL_MAHRIE", new String[] { "ID", "COL_SAL",
            "COL_NERKH" }, null, null, null, null, null);
    if (c.moveToFirst()) {

        shakhes = new ArrayList<Shakhes>();
        do {
            Shakhes p = new Shakhes();

            // p.setId((int) c.getLong(c.getColumnIndex("ID")));
            p.setSal(c.getString(c.getColumnIndex("COL_SAL")));
            p.setNerkh(c.getString(c.getColumnIndex("COL_NERKH")));

            shakhes.add(p);
        } while (c.moveToNext());
    }

    return shakhes;
}
}

Mahrie_JadvalActivity.java

package ir.dadpardaz.mahrie_dadpardaz;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Mahrie_JadvalActivity extends Activity {

    ListView personListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mahrie__jadval);

    personListView = (ListView) findViewById(R.id.personListView);
    DataBaseHandler databaseAdapter = new DataBaseHandler(this);        


    Shakhes shakhes1 = new Shakhes("1392" , "250");
    databaseAdapter.insertShakhes(shakhes1);

    Shakhes shakhes2 = new Shakhes("1391" , "200");
    databaseAdapter.insertShakhes(shakhes2);

    Shakhes shakhes3 = new Shakhes("1390" , "150");
    databaseAdapter.insertShakhes(shakhes3);

    Shakhes shakhes4 = new Shakhes("1389" , "100");
    databaseAdapter.insertShakhes(shakhes4);

    Shakhes shakhes5 = new Shakhes("1388" , "50");
    databaseAdapter.insertShakhes(shakhes5);

    Shakhes shakhes6 = new Shakhes("1387" , "25");
    databaseAdapter.insertShakhes(shakhes6);        

    ////


    List<Shakhes> shakhes = databaseAdapter.getAllShakhes() ;
    ShakhesListViewAdapter shakhesListViewAdapter = new ShakhesListViewAdapter(this, R.layout.mahrie_list_view_item, shakhes);
    personListView.setAdapter(shakhesListViewAdapter);



}

public class ShakhesListViewAdapter extends ArrayAdapter<Shakhes> {

    List<Shakhes> data;
    Context context;

    public ShakhesListViewAdapter(Context context, int resourceId, List<Shakhes> data) {
        super(context, resourceId, data);
        this.data = data;
        this.context = context;
    }

    @Override
    public View getView(final int position, View item, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        item = inflater.inflate(R.layout.mahrie_list_view_item, null);

        TextView familyTextView = (TextView) item.findViewById(R.id.familyTextView);
        familyTextView.setText(data.get(position).getSal()) ;
        //familyTextView.setTypeface(bYekan);

        TextView nameTextView = (TextView) item.findViewById(R.id.nameTextView);
        nameTextView.setText(data.get(position).getNerkh()) ;
        //nameTextView.setTypeface(bYekan);

        Button detailsButton = (Button) item.findViewById(R.id.detailsButton);
        detailsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, 
                        data.get(position).getSal() + " " + data.get(position).getNerkh(),
                        Toast.LENGTH_LONG).show();
            }
        });

        //item.startAnimation(animation);

        return item;
    }       

} }

我该如何解决这个问题?

0 个答案:

没有答案