jeudi 25 décembre 2014

Getting error while storing json data into SQLite

i am creating a registration form and getting error while storing data into SQLite. My data is inserting in MYSql but not in SQLite.In the below code as you can see in doinbackground function data is going to MYSql database but after that it generates error and data is not inserting in SQLite Here is my Registration code below.



protected String doInBackground(String... params) {

// TODO Auto-generated method stub


String firstname =edt_fname.getText().toString();
String email =edt_email.getText().toString();
String phone =edt_phone.getText().toString();
String address =edt_address.getText().toString();
String lastname =edt_lastName.getText().toString();
password =edt_password.getText().toString();
gender= rb.getText().toString();

List<NameValuePair> params1 = new ArrayList<NameValuePair>();

params1.add(new BasicNameValuePair("firstname", firstname));
params1.add(new BasicNameValuePair("lastname", lastname));
params1.add(new BasicNameValuePair("password", password));
params1.add(new BasicNameValuePair("email", email));
params1.add(new BasicNameValuePair("phone", phone));
params1.add(new BasicNameValuePair("address",address));
params1.add(new BasicNameValuePair("gender", gender));

Log.d("param",params1.toString());

json = jsonParser.makeHttpRequest(url_create_product,"POST", params1);

Log.d("Create Response", json.toString());

try {

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("detail");


db.addUser(json_user.getString(firstname),
json_user.getString(lastname),

json_user.getString(email),
json_user.getString(phone),
json_user.getString(address),
json_user.getString(gender),
json_user.getString(password));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
// registerErrorMsg.setText("Error occured in registration");
}

}catch (Exception e) {
// TODO: handle exception
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done

pDialog.dismiss();


this is my SQlite code below



public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "restaurant";

// Login table name
private static final String TABLE_LOGIN = "login";

// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_First_NAME = "firstname";
private static final String KEY_EMAIL = "email";
private static final String KEY_LastName = "lastname";
private static final String KEY_Password = "password";
private static final String KEY_Phone = "phone";
private static final String KEY_Address = "address";
private static final String KEY_Gender = "gender";




public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_First_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_LastName + " TEXT,"
+ KEY_Password + " TEXT,"
+ KEY_Phone + " TEXT,"
+ KEY_Address + " TEXT,"
+ KEY_Gender + " TEXT,"
+ ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.e("CREATE_LOGIN_TABLE", CREATE_LOGIN_TABLE.toString());
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);

// Create tables again
onCreate(db);
}

/**
* Storing user details in database
* */
public void addUser(String firstname, String email, String address,
String lastname, String password, String phone, String gender) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_First_NAME, firstname);
values.put(KEY_EMAIL, email);
values.put(KEY_Address, address);
values.put(KEY_LastName, lastname);
values.put(KEY_Password, password);
values.put(KEY_Phone, phone);// Name
values.put(KEY_Gender, gender); // Email



Log.e("values print ", values.toString());
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection

}

/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("firstname", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("lastname", cursor.getString(3));
user.put("password", cursor.getString(4));
user.put("phone", cursor.getString(5));
user.put("address", cursor.getString(6));
user.put("gender", cursor.getString(7));

Log.e("user which", user.toString());

/*user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));*/
}
cursor.close();
db.close();
// return user
return user;
}

/**
* Getting user login status
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();

// return row count
return rowCount;
}

/**
* Re crate database
* Delete all tables and create them again
* */
public void resetTables(){
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_LOGIN, null, null);
db.close();
}

}

Aucun commentaire:

Enregistrer un commentaire