vendredi 26 décembre 2014

error create sqlite database?

i want make a databases in sqlite, but when aplication run and i fill edit text and push button force close,


log caat error


12-27 09:36:23.063: E/Database(450): Failure 1 (near "TABLElatihan": syntax error) on 0x584160 when preparing 'CREATE TABLElatihan(_id INTEGER PRIMARY KEY,name TEXT,D TEXT)'.


12-27 09:36:23.183: E/AndroidRuntime(450): FATAL EXCEPTION: main 12-27 09:36:23.183: E/AndroidRuntime(450): android.database.sqlite.SQLiteException: near "TABLElatihan": syntax error: 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();

}

}


this is main activity



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);

}
});
}
}

Aucun commentaire:

Enregistrer un commentaire