Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
5
Answers
problem in insert into sqlite db ?
ghasem deh
8y
310
1
Reply
hi guy's
when try insert new data in database send this error : "attached database stopped"
this database class :
public
boolean
insertUser(String userName,
int
age,
int
gender,
int
password, String memDescription, String pic) {
User user =
null
;
openDatabase();
db.beginTransaction();
try
{
ContentValues values =
new
ContentValues();
values.put(
"userName"
, user.getUserName());
values.put(
"age"
, user.getAge());
values.put(
"gender"
, user.getGender());
values.put(
"password"
, user.getPassword());
values.put(
"memDescription"
, user.getMemDescription());
values.put(
"pic"
, user.getPic());
long
result = db.insertOrThrow(
"tblUsers"
,
null
, values);
db.setTransactionSuccessful();
closeDatabase();
if
(result == -
1
)
return
false
;
else
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
Log.d(TAG,
"???? ??? ?? ???????"
);
return
false
;
}
finally
{
db.endTransaction();
closeDatabase();
}
}
this in activity :
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
try
{
boolean
isInserted = DB_HELPER.insertUser(EDT_NAME.getText().toString(), Integer.parseInt(EDT_AGE.getText().toString()),
Integer.parseInt(EDT_GENDER.getText().toString()), Integer.parseInt(EDT_PASS.getText().toString()),
EDT_DESC.getText().toString(), EDT_PIC.getText().toString());
if
(isInserted ==
true
)
startActivity(
new
Intent(AddActivity.
this
, MainActivity.
class
));
else
Snackbar.make(view,
"??? ?? ??? ????? ????!"
, Snackbar.LENGTH_LONG).setAction(
"Action"
,
null
).show();
}
catch
(Exception e){
e.printStackTrace();
return
;
}
}
});
and this user table class :
public
class
User {
private
int
id;
private
String userName;
private
int
age;
private
int
gender;
private
int
password;
private
String memDescription;
private
String pic;
public
User(
int
id, String userName,
int
age,
int
gender,
int
password, String memDescription, String pic) {
this
.id = id;
this
.userName = userName;
this
.age = age;
this
.gender = gender;
this
.password = password;
this
.memDescription = memDescription;
this
.pic = pic;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getUserName() {
return
userName;
}
public
void
setUserName(String userName) {
this
.userName = userName;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
public
int
getGender() {
return
gender;
}
public
void
setGender(
int
gender) {
this
.gender = gender;
}
public
int
getPassword() {
return
password;
}
public
void
setPassword(
int
password) {
this
.password = password;
}
public
String getMemDescription() {
return
memDescription;
}
public
void
setMemDescription(String memDescription) {
this
.memDescription = memDescription;
}
public
String getPic() {
return
pic;
}
public
void
setPic(String pic) {
this
.pic = pic;
}
}
Post
Reset
Cancel
Answers (
5
)
Next Recommended Forum
Android Emulator running 5.1 crashes at boot
Realtime Time Application