Tuesday, 3 September 2013

i cannot add date value in to SQLite database it is showing error in the catalog ...please suggest the correct method

i cannot add date value in to SQLite database it is showing error in the
catalog ...please suggest the correct method

package com.example.sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
Button b1;
EditText et1;
DatabaseHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat formatter=new SimpleDateFormat("dd/mm/yyyy");
Date dateobject;
String date_var=(et1.getText().toString());
try {
dateobject=formatter.parse(date_var);
String date=new SimpleDateFormat().format(dateobject);
helper.AddDetail(date);
Toast.makeText(MainActivity.this,""+date, Toast.LENGTH_SHORT).show();
helper.close();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
if i use the method of helper.AddDetail(date) of DatabaseHelper it is
showing error at that method.please give solution with in this format only
so that i can understand easilyyy...
package com.example.sample;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
static final int VERSION=2;
static final String DATA_NAME="Mydata";
Context c;
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DATA_NAME, factory, VERSION);
c=context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String query="create table if not exists Mytable(id integer auto
increment,Daate text not null)";
db.execSQL(query);
}
public long AddDetail(String date)
{
db=getWritableDatabase();
ContentValues vals=new ContentValues();
vals.put("Daate",date);
long a=db.insert("Mytable", null, vals);
db.close();
return a;
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}

No comments:

Post a Comment