CodeforLife
دوشنبه 05 مهر 1395, 22:29 عصر
سلام من این کدها رو نوشتم برای درج ولی متاسفانه یه خطادارم و هر چه قدر هم که بررسی کردم چیزی پیدا نکردم
دوستان عزیزان لطفا راهنماییم کنید که ایراد کار کجاست ؟؟؟
ممنون
//cTor()
public Person(Context context) {
super(context, DB_NAME, null, DB_vERSION);
}
//
//peron Properties
//
//firstname
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//last name
private String lastName;
public String getlastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//phoen
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
//mobile
private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
//address
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
SQLiteDatabase db;
//add method
public Boolean addPersonInfo() {
Boolean b = false;
try {
ContentValues contentValues = new ContentValues();
contentValues.put("firstname", firstName);
contentValues.put("lastname", lastName);
contentValues.put("phone", phone);
contentValues.put("mobile", mobile);
contentValues.put("address", address);
long res = db.insert(CREATE_PERSON_TABLE, null, contentValues);
if (res == -1) {
b = false;
} else {
b = true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return b;
}
//DB Properties
static final String DB_NAME = "phoneDB";
static final String TBL_NAME = "personTbl";
static final int DB_vERSION = 1;
static final String CREATE_PERSON_TABLE = "CREATE TABLE " + TBL_NAME + "(Id INTEGER PRIMARY KEY AUTOINCREMENT ," +
" FIRSTNAME TEXT NOT NULL ," +
" LASTNAME TEXT ," +
" PHONE TEXT ," +
" MOBILE TEXT ," +
" ADDRESS TEXT )";
//
//Open DB
//
public Person openDB() {
try {
db = this.getWritableDatabase();
} catch (SQLException ex) {
ex.printStackTrace();
}
return this;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_PERSON_TABLE);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST" + TBL_NAME);
onCreate(db);
}
public void closeDB() {
this.close();
}
}
//
//initialize
//
edtFirstName = (EditText) findViewById(R.id.edtFirstName);
edtLastName = (EditText) findViewById(R.id.edtLastName);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMobile = (EditText) findViewById(R.id.edtMobile);
edtAddress = (EditText) findViewById(R.id.edtAddress);
btnSave = (Button) findViewById(R.id.btnSave);
//
//Event Save
//
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//db
Person person = new Person(G.context);
person.openDB();//open
//set value
person.setFirstName(edtFirstName.getText().toStrin g());
person.setLastName(edtLastName.getText().toString( ));
person.setPhone(edtPhone.getText().toString());
person.setMobile(edtMobile.getText().toString());
person.setAddress(edtAddress.getText().toString()) ;
//add method
Boolean res = person.addPersonInfo();
if (res == true) {
edtAddress.setText("");
edtFirstName.setText("");
edtLastName.setText("");
edtMobile.setText("");
edtPhone.setText("");
} else {
Toast.makeText(G.context, "error", Toast.LENGTH_SHORT).show();
}
person.close();//close
}
});
دوستان عزیزان لطفا راهنماییم کنید که ایراد کار کجاست ؟؟؟
ممنون
//cTor()
public Person(Context context) {
super(context, DB_NAME, null, DB_vERSION);
}
//
//peron Properties
//
//firstname
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//last name
private String lastName;
public String getlastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//phoen
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
//mobile
private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
//address
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
SQLiteDatabase db;
//add method
public Boolean addPersonInfo() {
Boolean b = false;
try {
ContentValues contentValues = new ContentValues();
contentValues.put("firstname", firstName);
contentValues.put("lastname", lastName);
contentValues.put("phone", phone);
contentValues.put("mobile", mobile);
contentValues.put("address", address);
long res = db.insert(CREATE_PERSON_TABLE, null, contentValues);
if (res == -1) {
b = false;
} else {
b = true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return b;
}
//DB Properties
static final String DB_NAME = "phoneDB";
static final String TBL_NAME = "personTbl";
static final int DB_vERSION = 1;
static final String CREATE_PERSON_TABLE = "CREATE TABLE " + TBL_NAME + "(Id INTEGER PRIMARY KEY AUTOINCREMENT ," +
" FIRSTNAME TEXT NOT NULL ," +
" LASTNAME TEXT ," +
" PHONE TEXT ," +
" MOBILE TEXT ," +
" ADDRESS TEXT )";
//
//Open DB
//
public Person openDB() {
try {
db = this.getWritableDatabase();
} catch (SQLException ex) {
ex.printStackTrace();
}
return this;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_PERSON_TABLE);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST" + TBL_NAME);
onCreate(db);
}
public void closeDB() {
this.close();
}
}
//
//initialize
//
edtFirstName = (EditText) findViewById(R.id.edtFirstName);
edtLastName = (EditText) findViewById(R.id.edtLastName);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMobile = (EditText) findViewById(R.id.edtMobile);
edtAddress = (EditText) findViewById(R.id.edtAddress);
btnSave = (Button) findViewById(R.id.btnSave);
//
//Event Save
//
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//db
Person person = new Person(G.context);
person.openDB();//open
//set value
person.setFirstName(edtFirstName.getText().toStrin g());
person.setLastName(edtLastName.getText().toString( ));
person.setPhone(edtPhone.getText().toString());
person.setMobile(edtMobile.getText().toString());
person.setAddress(edtAddress.getText().toString()) ;
//add method
Boolean res = person.addPersonInfo();
if (res == true) {
edtAddress.setText("");
edtFirstName.setText("");
edtLastName.setText("");
edtMobile.setText("");
edtPhone.setText("");
} else {
Toast.makeText(G.context, "error", Toast.LENGTH_SHORT).show();
}
person.close();//close
}
});