错误代码:1(SQLITE_ERROR)引起:SQL(查询)错误或缺少数据库

时间:2016-09-07 14:50:34

标签: android database listview android-studio

点击小吃栏我想将页面添加到收藏夹列表,但是当我点击小吃栏时我遇到了这个错误:

  

错误代码:1(SQLITE_ERROR)                                                                            引起:SQL(查询)错误或缺少数据库。                                                                             (没有这样的表:ghavaed(代码1):,编译时:INSERT INTO   ghavaed(saal,title)VALUES(?,?))

这是我的第一个项目请帮忙

这是我要添加到收藏列表的页面类:

public class StyleGhaede extends AppCompatActivity {

String title, tozih, sarfasl, saal;
TextView style_ghaede_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    title = extras.getString("title");
    tozih = extras.getString("tozih");
    sarfasl = extras.getString("sarfasl");
    saal = extras.getString("saal");
    setContentView(R.layout.activity_style_ghaede);

    getComponent();
    style_ghaede_content.setText(tozih);
}

private void getComponent(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(title);

    final DataBase db=new DataBase(this);
    final ghavaed fq=new ghavaed();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "", Snackbar.LENGTH_LONG)
                    .setAction("برای اضافه کردن به لیست علاقه مندی کلیک کنید", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            db.addToFavourite(fq.getSaal(),fq.getTitle());
                        }
                    }).show();
        }
    });

    style_ghaede_content = (TextView) findViewById(R.id.style_ghaede_content);
}
}

我喜爱的课程:

public class Favourite_List extends AppCompatActivity {

List<ghavaed> favouritelist;
DataBase db=new DataBase(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favourite__list);
    favouritelist=db.getFavouriteqavaed();
    ListView listview = (ListView) findViewById(R.id.list_view_fav);
    final Adapter_favourite adapter_fav = new Adapter_favourite(getApplicationContext(),favouritelist);
    listview.setAdapter(adapter_fav);
    ghavaed fq = new ghavaed();

    //swipelist
    SwipeLayout swipeLayout =  (SwipeLayout)findViewById(R.id.fav_item);
    swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
    swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));
    swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
        @Override
        public void onClose(SwipeLayout layout) {
            //when the SurfaceView totally cover the BottomView.
        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
            //you are swiping.
        }

        @Override
        public void onStartOpen(SwipeLayout layout) {
        }

        @Override
        public void onOpen(SwipeLayout layout) {
            //when the BottomView totally show.
        }

        @Override
        public void onStartClose(SwipeLayout layout) {
        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
            //when user's hand released.
        }
    });
}
}

我的数据库:

public class DataBase extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Database";
private static final String TABLE_FAVOURITE_GHAVAED = "ghavaed";

private static final String Tozih = "tozih";
private static final String Saal = "saal";
private static final String Title = "title";
private static final String TAG = "DataBase";

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

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_FAVOURITE_GHAVAED_TABLE = "CREATE TABLE " + TABLE_FAVOURITE_GHAVAED + "("
            + Saal + " TEXT,"
            + Title + " TEXT" + ")";
    db.execSQL(CREATE_FAVOURITE_GHAVAED_TABLE);
}

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

public void addToFavourite(String saal, String title) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Saal, saal); // Product Name
    values.put(Title, title); // Product Number
    // Inserting Row
    db.insert(TABLE_FAVOURITE_GHAVAED, null, values);
    db.close(); // Closing database connection
    // Log.i("setUserExam", "addToFavourite : done!!");
}

public void removeFavourite(String saal, String title)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM  " + TABLE_FAVOURITE_GHAVAED + " WHERE saal ='" + saal + "' AND title = '" + title + "' ");
    db.close();
}

public List<ghavaed> getFavouriteqavaed()
{
    List<ghavaed> favouriteqavaedList = new ArrayList<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM ghavaed", null);
    if (cursor.moveToFirst()) {
        do {
            ghavaed favouriteqavaed = new ghavaed();
            favouriteqavaed.setSaal(cursor.getString(0));
            favouriteqavaed.setTitle(cursor.getString(3));
           // favouriteqavaed.setTozih(cursor.getString(2));

            // Adding contact to list
            favouriteqavaedList.add(favouriteqavaed);
        } while (cursor.moveToNext());
    }
    db.close();
    return favouriteqavaedList;
}

public Integer getFavouritesNumber()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM ghavaed", null);
    return cursor.getCount();
}

public boolean checkIsFavourite(String saal, String title)
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM ghavaed WHERE saal = '" + saal + "'" +
            "and title = '" + title + "' ", null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        db.close();
        return true;
    } else {
        db.close();
        return false;
    }
}
}

public class ghavaed {
String saal, sarfasl, tozih, title;

public String getSaal() {
    return saal;
}

public void setSaal(String saal) {
    this.saal = saal;
}

public String getSarfasl() {
    return sarfasl;
}

public void setSarfasl(String sarfasl) {
    this.sarfasl = sarfasl;
}

public String getTozih() {
    return tozih;
}

public void setTozih(String tozih) {
    this.tozih = tozih;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

0 个答案:

没有答案