SQLite异常:没有像table这样的东西

时间:2015-06-27 16:10:04

标签: android sqlite

我在Android Studio中读取了一个简单的数据库,但是当我点击“保存”按钮时,它会抛出“SQLite异常:没有像表一样”的异常。我无法理解问题出在哪里? 我在下面分享代码和logcat。谢谢你的帮助。

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)

1 个答案:

答案 0 :(得分:0)

检查您的创建表,这里需要空格:

$.each

在这里

ID + "INTEGER 

尝试使用此功能:

 NAME + "VARCHAR(255)"