Why do I fail to save data in my application? - java

I am a student and started working on android studio recently. I don't know about it much. I am working on an application where I save the item name and its amount in the database and display toast message if data we entered is saved or not. problem is whenever I click on the save button my application crashes.
following is my DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Items.db";
public static final String TABLE_NAME = "item_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ITEM";
public static final String COL_3 = "AMOUNT";
public DatabaseHelper(#Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item, String amount){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,item);
contentValues.put(COL_3,item);
long result = db.insert(TABLE_NAME,null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
}
following is my MainActivity class:
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editItem, editAmount;
Button buttonSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editItem = (EditText)findViewById(R.id.item_field);
editAmount = (EditText)findViewById(R.id.amount_field);
buttonSave = (Button)findViewById(R.id.button_save);
AddData();
}
public void AddData(){
buttonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String item = editItem.getText().toString();
String amount = editAmount.getText().toString();
boolean insertData = myDb.addData(item, amount);
if(insertData == true){
Toast.makeText(MainActivity.this,"Amount is saved with Item detail", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "Error occurred : Detailed are not saved", Toast.LENGTH_LONG).show();
}
}
});
}
}
I will appreciate your help.
Thank you

It might be crashing because it's not creating the table:
String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)";
A space is missing between table and its name:
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEM TEXT, AMOUNT TEXT)";
You should also close the database after you get the data in db.insert but this only creates a warning:
long result = db.insert(TABLE_NAME,null, contentValues);
db.close();

Related

Problem with inserting new record to sqlite in android studio

I want to insert a new record into a sqlite db.
My table is called words. It has the columns id, word, meaning, details, lesson, ticks.
When I try to insert a new record, it just saves details as word and lesson as meaning, whereas details and lesson are stored with a null value.
The autoincrement primary key id and ticks are stored correctly.
Code
Handler:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
protected static final String DATABASE_NAME = "wordDatabase";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE words " +
"( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"word TEXT, " +
"meaning TEXT, " +
"details TEXT, " +
"lesson TEXT, " +
"ticks INTEGER ) ";
db.execSQL(sql);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS words";
db.execSQL(sql);
onCreate(db);
}
Object:
public class ObjectStudent {
int id;
String word;
String meaning;
String details;
String lesson;
int ticks;
public ObjectStudent(){
}
OnClickListener:
public class OnClickeListenerCreateStudent implements View.OnClickListener {
#Override
public void onClick(View view) {
final Context context = view.getRootView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
final EditText et_word = (EditText) formElementsView.findViewById(R.id.et_word);
final EditText et_meaning = (EditText) formElementsView.findViewById(R.id.et_meaning);
final EditText et_details = (EditText) formElementsView.findViewById(R.id.et_details);
final EditText et_lesson = (EditText) formElementsView.findViewById(R.id.et_lesson);
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Create Word")
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String wordTitle = et_word.getText().toString();
String wordMeaning = et_meaning.getText().toString();
String wordDetails = et_details.getText().toString();
String wordLesson = et_lesson.getText().toString();
ObjectStudent objectStudent = new ObjectStudent();
/* objectStudent.word= wordTitle;
objectStudent.meaning= wordMeaning;
objectStudent.details= wordDetails;
objectStudent.lesson= wordLesson;*/
objectStudent.word = "word"; // et_word.getText().toString();
objectStudent.meaning = "meaning"; // et_meaning.getText().toString();
objectStudent.details = "details"; // et_details.getText().toString();
objectStudent.lesson = "lesson"; // et_lesson.getText().toString();
objectStudent.ticks= 1;
boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
if(createSuccessful){
Toast.makeText(context, "Word information was saved.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to save Word information.", Toast.LENGTH_SHORT).show();
}
((MainActivity) context).countRecords();
((MainActivity) context).readRecords();
dialog.cancel();
}
}).show();
}
Create method on TableControllerStudent class:
public class TableControllerStudent extends DatabaseHandler {
public TableControllerStudent(Context context) {
super(context);
}
public boolean create(ObjectStudent objectStudent) {
ContentValues values = new ContentValues();
/* values.put("word", objectStudent.word);
values.put("meaning", objectStudent.meaning);
values.put("details", objectStudent.details);
values.put("lesson", objectStudent.lesson);*/
values.put("word", "w");
values.put("meaning", "m");
values.put("details", "d");
values.put("lesson", "l");
values.put("ticks", objectStudent.ticks);
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO " + "words "+ "(word, meaning,details, lesson, ticks ) VALUES ('word','meaning','details','lesson',2)");
//boolean createSuccessful = db.insert("words", null, values) > 0;
db.close();
//return createSuccessful;
return true;
}
Just update Database version to higher version(instead of 1,use 5).And check it.before run the application ,please uninstall and then run.
Use
db.insert(YOUR_TABLE_NAME,values);
instead of
db.execSQL() function

How to update a password in a SQLite database (Login and Registration System)? [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
When I click Submit Button in Change password class or layout the app crashes. What's wrong with my code, please teach me! Thanks in Advance. I am working with Sqlite database for user login system and user can change their password from app bar layout. On clicking >>change password in App Bar new activity pops up i.e changePassword Activity. Upto here everything works fine but after typing password and clicking on submit button the app crashes and sets back to MainActivity.
Here is my change password class
public class changePasswordActivity extends AppCompatActivity {
EditText oldpasswordEditText;
EditText newpasswordEditText;
EditText confirmpasswordEditText;
Button btnsubmit;
String realusername, realpassword;
String checkoldpass, checknewpass, checkconfirmpass;
SQLiteHelper sqLiteHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_password);
oldpasswordEditText = findViewById(R.id.oldpasswordEditText);
newpasswordEditText = findViewById(R.id.newPasswordEditText);
confirmpasswordEditText = findViewById(R.id.confirmPasswordEditText);
btnsubmit= findViewById(R.id.btnsubmit);
checkoldpass = oldpasswordEditText.getText().toString();
checknewpass = newpasswordEditText.getText().toString();
checkconfirmpass = confirmpasswordEditText.getText().toString();
realusername = getIntent().getStringExtra("USERNAME");
realpassword= getIntent().getStringExtra("PASSWORD");
btnsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!(checkconfirmpass.equals(checknewpass))) {
Toast.makeText(changePasswordActivity.this,"New Password didn't matched",Toast.LENGTH_SHORT).show();
}else if (!(realpassword.equals(checkoldpass))){
Toast.makeText(changePasswordActivity.this,"Old password didn't matched",Toast.LENGTH_SHORT).show();
}
else if (checkconfirmpass.equals(checkoldpass)){
Toast.makeText(changePasswordActivity.this,"New password cannot be same as old password",Toast.LENGTH_SHORT).show();
}else if(checkconfirmpass.equals(checknewpass.equals(checkoldpass))) {
Toast.makeText(changePasswordActivity.this, "New password cannot be same as old password", Toast.LENGTH_SHORT).show();
}
else{
sqLiteHelper = new SQLiteHelper(changePasswordActivity.this);
sqLiteHelper.changepassword(checknewpass,realusername);
}
}
});
}
}
Here is my SQLiteHelper class
public class SQLiteHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
private static final String DATABASE_NAME = "info.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "profile";
public static final String COLUMN_ID = "userid";
public static final String COLUMN_FULLNAME = "fullname";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_MOBILE = "mobile";
private static final String CREATE_TABLE_QUERY =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_FULLNAME + " TEXT, " +
COLUMN_EMAIL + " TEXT, " +
COLUMN_PASSWORD + " TEXT, " +
COLUMN_MOBILE + " TEXT " + ")";
//modified constructor
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_QUERY);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public Cursor getData() {
String query = "SELECT * FROM" + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
public void changepassword(String mpassword, String mname) {
Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
+SQLiteHelper.COLUMN_PASSWORD
+" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}
}
db will be null, there is also no need for a Cursor, so change
public void changepassword(String mpassword, String mname) {
Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
+SQLiteHelper.COLUMN_PASSWORD
+" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}
To :-
public void changepassword(String mpassword, String mname) {
SQLiteDatabase db = this.getWriteableDatabae();
db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
+SQLiteHelper.COLUMN_PASSWORD
+" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}
You will also have the same issue with the getData method so you should add SQLiteDatabase db = this.getWriteableDatabae(); to the getData method as well.
Alternately you could change :-
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
to :-
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWriteableDatabase();
}
In which case db will be a valid SQLiteDatabase.

SQLITE DATABASE error in displaying data to listview

im getting problem in displaying my data from sqlite db
my apps stops working when run and display no errors.
please help me
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "ExpDatee.db";
public static final String TABLE_NAME = "EXPDATE_TABLE";
public static final String COL_1 = "PRO_ID";
public static final String COL_2 = "PRO_NAME";
public static final String COL_3 = "PRO_EXPDATE";
public static final String COL_4 = "PRO_DAYTILLEXP";
/**
private static final String SQL_CREATE_TABLE_EXPDATE = "CREATE TABLE " + TABLE_NAME + "("
+ PRO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PRO_NAME + " TEXT NOT NULL, "
+ PRO_EXPDATE+ " TEXT NOT NULL, "
+ PRO_DAYTILLEXP + " TEXT NOT NULL, "
+ ");";
**/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
// SQLiteDatabase db = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(PRO_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"PRO_NAME TEXT, " +
"PRO_EXPDATE TEXT," +
" PRO_DAYTILLEXP TEXT )");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
//method to insert data
public boolean insertData(String PRO_NAME, String PRO_EXPDATE, String PRO_DAYTILLEXP)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,PRO_NAME);
contentValues.put(COL_3,PRO_EXPDATE);
contentValues.put(COL_4,PRO_DAYTILLEXP);
Log.d(TAG, "ADD DATA : ADDING " + PRO_NAME + " TO " + TABLE_NAME);
Log.d(TAG, "ADD DATA : ADDING " + PRO_EXPDATE + " TO " + TABLE_NAME);
Log.d(TAG, "ADD DATA : ADDING " + PRO_DAYTILLEXP + " TO " + TABLE_NAME);
long result = db.insert(TABLE_NAME,null ,contentValues );
if (result == -1)
return false;
else
return true;
}
//get all data
public Cursor getData()
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM" + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
EditText etProductName, etDaysBeforeExp, etExpDate;
Button btnAddItem, btnViewItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new DatabaseHelper(this);
etProductName = (EditText)findViewById(R.id.etProductName);
etDaysBeforeExp = (EditText)findViewById(R.id.etDaysBeforeExp);
etExpDate = (EditText)findViewById(R.id.etExpDate);
btnAddItem = (Button)findViewById(R.id.btnAddItem);
btnViewItem = (Button)findViewById(R.id.btnViewItem);
btnAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String PRO_NAME = etProductName.getText().toString();
String PRO_EXPDATE = etExpDate.getText().toString();
String PRO_DAYTILLEXP = etDaysBeforeExp.getText().toString();
if(etProductName.length() !=0)
{
AddData(PRO_NAME,PRO_EXPDATE,PRO_DAYTILLEXP);
etProductName.setText("");
etExpDate.setText("");
etDaysBeforeExp.setText("");
}
else
{
toastMessage("PLEASE INSERT VALUE");
}
}
});
btnViewItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
});
}
public void AddData(String PRO_NAME, String PRO_EXPDATE, String PRO_DAYTILLEXP)
{
boolean InsertData = myDB.insertData(PRO_NAME,PRO_EXPDATE,PRO_DAYTILLEXP);
if(InsertData)
{
toastMessage("DATA INSERTED");
}
else
{
toastMessage("DATA NOT INSERTED");
}
}
private void toastMessage(String message) {
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}
}
ListDataActivity.java
public class ListDataActivity extends AppCompatActivity {
DatabaseHelper myDB;
private ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
populateListView();
}
private void populateListView() {
Cursor data = myDB.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext())
{
listData.add(data.getString(1));
listData.add(data.getString(2));
listData.add(data.getString(3));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
}
private void toastMessage(String message) {
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}
}
Initialized Listdataactivity
myDB = new DatabaseHelper(this);
Use Arrayadapter
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);

every time creating database after insert a row in android sql

#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int Database_version = 2;
public static final String Tag = DatabaseOperations.class.getSimpleName();
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TableData.TableInfo.TABLE_NAME + " (" +
TableData.TableInfo.USER_ID + " INTEGER PRIMARY KEY," +
TableData.TableInfo.USER_PASS +" TEXT "+ "," +
TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null,Database_version);
Log.d("Tag", "Database created");
}
#Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(SQL_CREATE_ENTRIES);
Log.d("Tag", "Table created");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void putInformation(DatabaseOperations drop, String name, String pass, String email) {
SQLiteDatabase SQ = drop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_ID, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
Log.d("Tag", "inert a row");
}
public Cursor getInformation(DatabaseOperations dop) {
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloumns = {TableData.TableInfo.USER_ID, TableData.TableInfo.USER_PASS, TableData.TableInfo.USER_EMAIL};
Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, coloumns, null, null, null, null, null);
return CR;
}
}
RegisterActivity
public class RegisterActivity extends AppCompatActivity {
EditText USER_NAME, USER_PASS, CON_PASS, USER_EMAIL;
String user_name, user_pass, con_pass, user_email;
Button REG;
Context ctx = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText) findViewById(R.id.reg_pass);
CON_PASS = (EditText) findViewById(R.id.con_pass);
USER_EMAIL = (EditText) findViewById(R.id.reg_email);
REG = (Button) findViewById(R.id.user_reg);
REG.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
user_email = USER_EMAIL.getText().toString();
if (!(user_pass.equals(con_pass))) {
Toast.makeText(getBaseContext(), "Password are not matching", Toast.LENGTH_LONG).show();
USER_NAME.setText("");
USER_PASS.setText("");
CON_PASS.setText("");
USER_EMAIL.setText("");
} else {
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, user_name, user_pass, user_email);
Toast.makeText(getBaseContext(), "Registration is suessful", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
It is because you are creating table each time you insert a row. To solve this problem you need to change the create table query "CREATE TABLE 'TABLE_NAME' IF NOT EXISTS". The "IF NOT EXISTS" will restrict the system to create the database again if it is created once.
You are calling creating a new Table every time, you create an instance of DatabaseOperations - as your SQL statement 'CREATES'
Modify SQL_CREATE_ENTRIES to something like below
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE IF NOT EXISTS " + TableData.TableInfo.TABLE_NAME + " (" +
TableData.TableInfo.USER_ID + + " integer primary key autoincrement, "+
TableData.TableInfo.USER_PASS +" TEXT "+ "," +
TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";
Edit
1) I've update query to include auto-increment your primary key
2) In your onUpgrade method add the line
db.execSQL("DROP TABLE IF EXISTS " + TableData.TableInfo.TABLE_NAME);
This will delete the table when you change the version of DB.
Next, update the version of by 1, to 3.
Re-run app, it will be rebuild the DB table.

While compile : Creation table error

While compiling, this error shows:
android.database.sqlite.SQLiteException: near "TABLEnewtable": syntax error (code 1): , while compiling: CREATE TABLEnewtable{id INTEGER PRIMERY KEY, editname TEXT, edittel TEXT, editskype TEXT, editaddress TEXT }.
public class DataManipulator {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_NAME = "newtable";
private static Context context;
static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into" +TABLE_NAME+ "(editname, edittel, editskype,
editaddress) values(?,?,?,?)";
public DataManipulator(Context context)
{
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getReadableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}
public long insert(String editname, String edittel, String editskype, String editaddress)
{
this.insertStmt.bindString(1, editname);
this.insertStmt.bindString(2, edittel);
this.insertStmt.bindString(3, editskype);
this.insertStmt.bindString(4, editaddress);
return this.insertStmt.executeInsert();
}
public void deleteAll()
{
db.delete(TABLE_NAME, null, null);
}
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[]{"id","editname", "edittel", "editskype",
"editaddress"},null, null, null, null, "name asc");
int x=0;
if(cursor.moveToFirst())
{
do {
String[] bb= new String[] {
cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
list.add(bb);
x=x+1;
}
while(cursor.moveToNext());
}
if(cursor != null && !cursor.isClosed())
{
cursor.close();
}
cursor.close();
return list;
}
public void delete(int rowId)
{
db.delete(TABLE_NAME, null, null);
}
private static class OpenHelper extends SQLiteOpenHelper
{
OpenHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE" +TABLE_NAME+ "{id INTEGER PRIMERY KEY, editname TEXT, edittel TEXT, editskype TEXT, editaddress TEXT }");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
}
You have a lot of basic SQL syntax problems. Please consider learning some basic SQL and stacktrace reading first.
Add whitespace between SQL keywords such as TABLE and identifiers such as newtable.
For example, change
"CREATE TABLE" +TABLE_NAME+
to
"CREATE TABLE " +TABLE_NAME+
and
"insert into" +TABLE_NAME+
to
"insert into " +TABLE_NAME+
The parentheses in CREATE TABLE should be ( ) and not { }
Typo in PRIMERY, should be PRIMARY.
Plus possibly a lots more; these are just the issues found with the first 10 seconds of looking at your SQL.
Check this :
db.execSQL("CREATE TABLE " +TABLE_NAME+ "(id INTEGER PRIMARY KEY, editname TEXT, edittel TEXT, editskype TEXT, editaddress TEXT )");

Categories

Resources