samedi 27 juin 2015

SQLite Exception : no such like table

I read a simply database in Android Studio but ıt's throw a exception that "SQLite Exception : no such like table" when I click save button. I cannot understand where is the problem ? I'm sharing codes and logcat in below. Thank you for your help.

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;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


public class Database{


    public static final String LOG_MESSAGE = "message";
    DatabaseHelper databaseHelper;
    public Database(Context context){
        databaseHelper = new DatabaseHelper(context);
    }

    public long insertData(String name){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.NAME, name);
        long id= db.insert(DatabaseHelper.TABLE_NAME ,null,contentValues);
        db.close();
        return id;
    }

    public String getAllData(){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String[] columns = {DatabaseHelper.ID, DatabaseHelper.NAME};
        Cursor cursor = db.query(DatabaseHelper.TABLE_NAME,columns,null,null,null,null,null);
        StringBuffer buffer = new StringBuffer();
        while(cursor.moveToNext()){
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            buffer.append(id +" "+name +"\n");
        }
        cursor.close();
        return buffer.toString();
    }



 static class DatabaseHelper extends SQLiteOpenHelper{

    //database tanımlamaları buradan başlar
    private static final String DATABASE_NAME = "veritabanim.db";
    private static final int DATABASE_VERSION = 3;
    private  static final String TABLE_NAME = "veritabani";
    private static final String ID = "_id";
    private static final String NAME = "name";
    private static final String CREATE_TABLE =
                   "CREATE TABLE " + TABLE_NAME +
                    "("
                    + ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + NAME + "VARCHAR(255)" +
                    "); ";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    private Context context;



    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context =context;
        Log.i(LOG_MESSAGE,"constructur");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //CREATE TABLE deneme2 (_id INTEGER PRIMARY KEY AUYOINCREMENT, name VARCHAR(255));
        //yazılması gereken sorgu yukarıdakidir.

        try {
            Log.i(LOG_MESSAGE,"oncreate");
            db.execSQL(CREATE_TABLE);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

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

        try {
            Log.i(LOG_MESSAGE,"upgrade");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
}

MainActivity

package com.example.ilk.deneme2veritabanislemleri;


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;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


public class Database{


    public static final String LOG_MESSAGE = "message";
    DatabaseHelper databaseHelper;
    public Database(Context context){
        databaseHelper = new DatabaseHelper(context);
    }

    public long insertData(String name){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.NAME, name);
        long id= db.insert(DatabaseHelper.TABLE_NAME ,null,contentValues);
        db.close();
        return id;
    }

    public String getAllData(){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String[] columns = {DatabaseHelper.ID, DatabaseHelper.NAME};
        Cursor cursor = db.query(DatabaseHelper.TABLE_NAME,columns,null,null,null,null,null);
        StringBuffer buffer = new StringBuffer();
        while(cursor.moveToNext()){
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            buffer.append(id +" "+name +"\n");
        }
        cursor.close();
        return buffer.toString();
    }



 static class DatabaseHelper extends SQLiteOpenHelper{

    //database tanımlamaları buradan başlar
    private static final String DATABASE_NAME = "veritabanim.db";
    private static final int DATABASE_VERSION = 3;
    private  static final String TABLE_NAME = "veritabani";
    private static final String ID = "_id";
    private static final String NAME = "name";
    private static final String CREATE_TABLE =
                   "CREATE TABLE " + TABLE_NAME +
                    "("
                    + ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + NAME + "VARCHAR(255)" +
                    "); ";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    private Context context;



    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context =context;
        Log.i(LOG_MESSAGE,"constructur");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //CREATE TABLE deneme2 (_id INTEGER PRIMARY KEY AUYOINCREMENT, name VARCHAR(255));
        //yazılması gereken sorgu yukarıdakidir.

        try {
            Log.i(LOG_MESSAGE,"oncreate");
            db.execSQL(CREATE_TABLE);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

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

        try {
            Log.i(LOG_MESSAGE,"upgrade");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
}

logcat

06-27 19:07:21.286  29194-29194/com.example.ilk.deneme2veritabanislemleri E/SQLiteLog﹕ (1) no such table: veritabani
06-27 19:07:21.286  29194-29194/com.example.ilk.deneme2veritabanislemleri E/SQLiteDatabase﹕ Error inserting name=yazı
    android.database.sqlite.SQLiteException: no such table: veritabani (code 1): , while compiling: INSERT INTO veritabani(name) VALUES (?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
            at com.example.ilk.deneme2veritabanislemleri.Database.insertData(Database.java:28)
            at com.example.ilk.deneme2veritabanislemleri.MainActivity$1.onClick(MainActivity.java:48)
            at android.view.View.performClick(View.java:4443)
            at android.view.View$PerformClick.run(View.java:18433)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5021)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
            at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire