I am trying to Insert data into Database, but getting problem, error says:
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
Error1 says:-
com.example.db.myDBClass.InsertData(myDBClass.java:51)
Line is:-
long rows = db.insert(TABLE_MEMBER, null, Val);
Error2 says:-
com.example.db.UploadActivity.InsertDatas(UploadActivity.java:271)
Line is:-
long saveStatus = myDb.InsertData(editImageName.getText().toString(),editPersonName.getText().toString(),
editPersonaEmail.getText().toString(), editPersonTelephone.getText().toString());
Error3 says:-
com.example.db.UploadActivity$3.onClick(UploadActivity.java:429)
Line is:-
InsertDatas();
Log:-
11-23 06:44:55.104: E/SQLiteLog(8297): (20) statement aborts at 6: [INSERT INTO members(PersonTelephone,ImageName,PersonName,PersonEmail) VALUES (?,?,?,?)] datatype mismatch
11-23 06:44:55.135: E/SQLiteDatabase(8297): Error inserting PersonTelephone=234234234324 ImageName=IMG_20131123_044559.jpg PersonName=ASDFDASFASDF [email protected]
11-23 06:44:55.135: E/SQLiteDatabase(8297): android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at com.example.db.myDBClass.InsertData(myDBClass.java:51)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at com.example.db.UploadActivity.InsertDatas(UploadActivity.java:271)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at com.example.db.UploadActivity$3.onClick(UploadActivity.java:429)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.view.View.performClick(View.java:4240)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.view.View$PerformClick.run(View.java:17721)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.os.Handler.handleCallback(Handler.java:730)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.os.Looper.loop(Looper.java:137)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at java.lang.reflect.Method.invoke(Method.java:525)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 06:44:55.135: E/SQLiteDatabase(8297): at dalvik.system.NativeStart.main(Native Method)
myDBClass.java:-
public class myDBClass extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_MEMBER = "members";
public myDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(ImageName TEXT(100)," +
" PersonName TEXT(100)," +
" PersonEmail TEXT(100)," +
" PersonTelephone TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully");
}
// Insert Data
public long InsertData(String strImageName, String strPersonName, String strPersonEmail, String strPersonTelephone) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
ContentValues Val = new ContentValues();
Val.put("ImageName", strImageName);
Val.put("PersonName", strPersonName);
Val.put("PersonEmail", strPersonEmail);
Val.put("PersonTelephone", strPersonTelephone);
long rows = db.insert(TABLE_MEMBER, null, Val);
db.close();
return rows; // return rows inserted.
} catch (Exception e) {
return -1;
}
}
// Select Data
public String[] SelectData(String strImageName) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" },
"ImageName=?",
new String[] { String.valueOf(strImageName) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
arrData[3] = cursor.getString(3);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
}
UploadActivity.java:-
public boolean InsertDatas()
{
// Dialog
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
AlertDialog ad = adb.create();
// Check MemberID
if(editImageName.getText().length() == 0)
{
editImageName.setError("Please Enter Image Name");
editImageName.requestFocus();
return false;
}
// Check Name
if(editPersonName.getText().length() == 0)
{
editPersonName.setError("Please Enter Person Name");
editPersonName.requestFocus();
return false;
}
// Check Tel
if(editPersonaEmail.getText().length() == 0)
{
editPersonaEmail.setError("Please Enter Person Email");
editPersonaEmail.requestFocus();
return false;
}
// Check Tel
if(editPersonTelephone.getText().length() == 0)
{
editPersonTelephone.setError("Please Enter Telephone Number");
editPersonTelephone.requestFocus();
return false;
}
// Save Data
long saveStatus = myDb.InsertData(editImageName.getText().toString(),
editPersonName.getText().toString(),
editPersonaEmail.getText().toString(), editPersonTelephone.getText().toString());
if(saveStatus <= 0)
{
ad.setMessage("Error!! ");
ad.show();
return false;
}
Toast.makeText(UploadActivity.this,"Add Data Successfully. ",
Toast.LENGTH_SHORT).show();
return true;
}
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InsertDatas();
}
});
See Question&Answers more detail:
os