0

I am trying to use sqlite in Android programming . This is my DBAdapter code.

    package com.vahe_muradyan.notes;

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

public class DBAdapter {

    private static final String TAG = DBAdapter.class.getSimpleName();
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_BODY = "notes";

    private DatabaseHelper DbHelper;
    public static SQLiteDatabase Db;

    private static final String DATABASE_NAME = "notes.db";
    private static final String TABLE_NAME = "notes";
    private static final int DATABASE_VERSION = 1;



    private final Context context;

    private static class DatabaseHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase arg0) {
            String sql = String
        .format("create table %s (%s int primary key, %s text, %s text)",
                TABLE_NAME, COLUMN_ID,
                COLUMN_TITLE,
                COLUMN_BODY);
            arg0.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            arg0.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(arg0);

        }

    }

    public DBAdapter(Context contextArg) {
        context = contextArg;
    }

    public DBAdapter open() throws SQLException {
        DbHelper = new DatabaseHelper(context);
        Db = DbHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        Db.close();
    }

    public long insertNote(String title, String text) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_TITLE, title);
        values.put(COLUMN_BODY, text);
        return Db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
    }

    public boolean deleteNote(long rowId) {
        return Db.delete(TABLE_NAME, COLUMN_ID + "=" + rowId, null) > 0;
    }

    public Cursor getAllNotes() {
        return Db.query(TABLE_NAME, new String[] { COLUMN_ID, COLUMN_TITLE,
                COLUMN_BODY }, null, null, null, null, null);
    }

    public Cursor getNote(long rowId) throws SQLException {
        Cursor cursor = Db.query(TABLE_NAME, new String[] { COLUMN_ID,
                COLUMN_TITLE, COLUMN_BODY }, COLUMN_ID + "=" + rowId, null,
                null, null, null, null);
        if(cursor !=null){
            cursor.moveToFirst();
        }
        return cursor;
    }
}

I am trying to create application for Notes . And when I call insertNote() the program throws exception

07-02 20:05:21.487: E/SQLiteLog(14648): (1) table notes has no column named notes

Here I call inserNote()

DbAdapter = new DBAdapter(getApplicationContext());
  DbAdapter.open();
  DbAdapter.insertNote(title, text);
  DbAdapter.close();
Vahe Muradyan
  • 1,115
  • 1
  • 11
  • 22

0 Answers0