2015-01-09 25 views
6

Ben sqlite ile çalışıyorum. Veritabanı ve tabloyu başarıyla oluşturdum. Ayrıca tablomda yeni değerler ekleyebilecek kod yazdım. Kodum mükemmel çalışıyor, ama şimdi şunu göstermek istiyorum: yeni bir değer eklediyse tost mesajı, tostda hata mesajı veya başka bir şey.android sqlite Buraya yeni bir tane eklenip eklenmediğini kontrol edin

public void InsertToPhysicalPersonTable(String FirstName, String LastName, 
     String FullName, String FatherName) { 
    try { 
     ContentValues newValues = new ContentValues(); 
     newValues.put("FirstName", FirstName); 
     newValues.put("LastName", LastName); 
     newValues.put("FullName", FullName); 
     newValues.put("FatherName", FatherName); 



     db.insert(AddNewPhysicalPerson, null, newValues); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
     Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show(); 
    } 

} 

Bu gibi benim işlev denir:: herkes çözümü bilen varsa

loginDataBaseAdapter.InsertToPhysicalPersonTable("FirstName", 
        "LastName", 
        "FullName", 
        "FatherName" 
        ); 

, bana yardım edin Bu tablo kaynak koduna bir benim insert olduğunu.

cevap

25

insert() yöntemi, yeni eklenen satırın satır kimliğini ya da bir hata oluştuğunda -1 değerini döndürür.

Değişim bu

long rowInserted = db.insert(AddNewPhysicalPerson, null, newValues); 
if(rowInserted != -1) 
    Toast.makeText(myContext, "New row added, row id: " + rowInserted, Toast.LENGTH_SHORT).show(); 
else 
    Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show(); 
1
long result = db.insert(table name, null, contentvalues); 
     if(result==-1) 
      return false; 
      else 
      return true; 

bu onun için iyi bir çözümdür gibi

db.insert(AddNewPhysicalPerson, null, newValues); 

..