ghasem deh

ghasem deh

  • NA
  • 258
  • 37.6k

problem in insert into sqlite db ?

Dec 22 2016 12:43 PM
hi guy's
when try insert new data in database send this error : "attached database stopped" 
 
this database class :
 
  1. public boolean insertUser(String userName, int age, int gender, int password, String memDescription, String pic) {  
  2.         User user = null;  
  3.         openDatabase();  
  4.         db.beginTransaction();  
  5.         try {  
  6.             ContentValues values = new ContentValues();  
  7.             values.put("userName", user.getUserName());  
  8.             values.put("age", user.getAge());  
  9.             values.put("gender", user.getGender());  
  10.             values.put("password", user.getPassword());  
  11.             values.put("memDescription", user.getMemDescription());  
  12.             values.put("pic", user.getPic());  
  13.             long result = db.insertOrThrow("tblUsers"null, values);  
  14.             db.setTransactionSuccessful();  
  15.             closeDatabase();  
  16.             if (result == -1)  
  17.                 return false;  
  18.             else  
  19.                 return true;  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.             Log.d(TAG, "???? ??? ?? ???????");  
  23.             return false;  
  24.         } finally {  
  25.             db.endTransaction();  
  26.             closeDatabase();  
  27.         }  
  28.     }  
this in activity :
 
  1. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
  2.         fab.setOnClickListener(new View.OnClickListener() {  
  3.             @Override  
  4.             public void onClick(View view) {  
  5.                 try {  
  6.                     boolean isInserted = DB_HELPER.insertUser(EDT_NAME.getText().toString(), Integer.parseInt(EDT_AGE.getText().toString()),  
  7.                             Integer.parseInt(EDT_GENDER.getText().toString()), Integer.parseInt(EDT_PASS.getText().toString()),  
  8.                             EDT_DESC.getText().toString(), EDT_PIC.getText().toString());  
  9.                     if (isInserted == true)  
  10.                         startActivity(new Intent(AddActivity.this, MainActivity.class));  
  11.                     else  
  12.                         Snackbar.make(view, "??? ?? ??? ????? ????!", Snackbar.LENGTH_LONG).setAction("Action"null).show();  
  13.                 } catch(Exception e){  
  14.                     e.printStackTrace();  
  15.                     return;  
  16.                 }  
  17.             }  
  18.         });  
and this user table class :
 
  1. public class User {  
  2.     private int id;  
  3.     private String userName;  
  4.     private int age;  
  5.     private int gender;  
  6.     private int password;  
  7.     private String memDescription;  
  8.     private String pic;  
  9.   
  10.     public User(int id, String userName, int age, int gender, int password, String memDescription, String pic) {  
  11.         this.id = id;  
  12.         this.userName = userName;  
  13.         this.age = age;  
  14.         this.gender = gender;  
  15.         this.password = password;  
  16.         this.memDescription = memDescription;  
  17.         this.pic = pic;  
  18.     }  
  19.   
  20.     public int getId() {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(int id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getUserName() {  
  29.         return userName;  
  30.     }  
  31.   
  32.     public void setUserName(String userName) {  
  33.         this.userName = userName;  
  34.     }  
  35.   
  36.     public int getAge() {  
  37.         return age;  
  38.     }  
  39.   
  40.     public void setAge(int age) {  
  41.         this.age = age;  
  42.     }  
  43.   
  44.     public int getGender() {  
  45.         return gender;  
  46.     }  
  47.   
  48.     public void setGender(int gender) {  
  49.         this.gender = gender;  
  50.     }  
  51.   
  52.     public int getPassword() {  
  53.         return password;  
  54.     }  
  55.   
  56.     public void setPassword(int password) {  
  57.         this.password = password;  
  58.     }  
  59.   
  60.     public String getMemDescription() {  
  61.         return memDescription;  
  62.     }  
  63.   
  64.     public void setMemDescription(String memDescription) {  
  65.         this.memDescription = memDescription;  
  66.     }  
  67.   
  68.     public String getPic() {  
  69.         return pic;  
  70.     }  
  71.   
  72.     public void setPic(String pic) {  
  73.         this.pic = pic;  
  74.     }  
  75. }  
 

Answers (5)