错误创建sqlite数据库?

时间:2014-12-27 02:55:44

标签: android database sqlite

我想在sqlite中创建一个数据库,但是当aplication运行并且我填充编辑文本并按下按钮强制关闭时,

日志错误

12-27 09:36:23.063:E /数据库(450):在准备' CREATE TABLElatihan(_id INTEGER PRIMARY KEY)时,在0x584160上失败1(接近" TABLElatihan":语法错误) ,命名为TEXT,D TEXT)'。

12-27 09:36:23.183:E / AndroidRuntime(450):致命异常:主要 12-27 09:36:23.183:E / AndroidRuntime(450):android.database.sqlite.SQLiteException:near" TABLElatihan&#34 ;:语法错误:CREATE TABLElatihan(_id INTEGER PRIMARY KEY,name TEXT,D TEXT )

package com.example.sqlitelatihan;

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

public class DatabaseHandler extends SQLiteOpenHelper{

    //VARIABEL
    //Database version
    private static final int DATABASE_VERSION = 1 ;

    //database name
    private static final String DATABASE_NAME = "dbLatihan";

    //table
    private static final String TABLE_LATIHAN = "latihan";

    //coloumn
    private static final String KEY_ID = "_id";
    private static final String KEY_NAME = "name";
    private static final String KEY_D = "D";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    //membuat table
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_LATIHAN_TABLE = "CREATE TABLE"+ TABLE_LATIHAN +"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_D+" TEXT"+")";
        db.execSQL(CREATE_LATIHAN_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        //drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS"+TABLE_LATIHAN);

        //Created table again
        onCreate(db);
    }

///add value
    public void addIsi (String nama, String D){
        SQLiteDatabase  db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME,"dapatNama");
        values.put(KEY_D, "dapatD");

        // insert row
        db.insert(TABLE_LATIHAN, null, values);
        db.close();

    }

}

这是主要活动

package com.example.sqlitelatihan;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


    public class MainActivity extends Activity {

        EditText nama,d;
        Button tekan;
        String dapatNama, dapatD;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final DatabaseHandler database = new DatabaseHandler(this);

            nama = (EditText) findViewById(R.id.editText1);
            d = (EditText) findViewById(R.id.editText2);
            tekan = (Button) findViewById (R.id.tombol);

            tekan.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                dapatNama = nama.getText().toString();
                dapatD = d.getText().toString();


            database.addIsi(dapatNama,dapatD);  

                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

中缺少空格
String CREATE_LATIHAN_TABLE = "CREATE TABLE"+ TABLE_LATIHAN +"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_D+" TEXT"+")";

"CREATE TABLE "

如果您阅读日志,它会告诉您语法错误,它甚至会告诉您错误。