插入并查看sqlite数据库错误

时间:2015-10-07 13:45:02

标签: android broadcastreceiver android-sqlite

我有一个广播接收器来检测传入的短信,并且我将sms存储在sqlite数据库中。然后我尝试使用查询查看数据库内容。当我没有使用" id INTEGER PRIMARY KEY AUTOINCREMENT"时,代码工作正常。添加" id"后,当我尝试查看数据库内容时,显示"未找到记录"。所以要么它没有正确输入值,要么在视图查询代码中有错误。我在其他帖子中搜索但无法找到错误。任何帮助都会非常有帮助。



Broadcastreceiver:

public class MyBroadcast  extends BroadcastReceiver{
	
Context mContext;	

String msg_body;    
String mob_no, name;
String dttm, key;
SQLiteDatabase db;


@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
	// TODO Auto-generated method stub
	
	db=context.openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
	
		 	 
	 Bundle bundle = intent.getExtras();
     if (bundle != null) {
      Object[] pdus = (Object[])bundle.get("pdus");
      final SmsMessage[] messages = new SmsMessage[pdus.length];
      for (int i = 0; i < pdus.length; i++) {
         messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
       }
          StringBuffer content = new StringBuffer();
           if (messages.length > 0) {
             for (int i = 0; i < messages.length; i++) {
               content.append(messages[i].getMessageBody());
               mob_no = messages[i].getOriginatingAddress();
               Calendar calendar = Calendar.getInstance();
		        Date finaldate = calendar.getTime();
		        dttm = finaldate.toString();  
               
             }
           }
           msg_body = content.toString();
           
           Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(mob_no));
           Cursor c = context.getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
           try {
               if(c.moveToFirst()){
               	name = c.getString(0);
               	
               	db.execSQL("INSERT INTO student (sender, timestamp, mesg) VALUES('"+name+"','"+dttm+"','"+msg_body+"');");
               
           		
               	
               }else{
               	
               	
               	db.execSQL("INSERT INTO student (sender,timestamp,mesg) VALUES('"+mob_no+"','"+dttm+"','"+msg_body+"');");
               }
               
               
           } catch (Exception e) {
               // TODO: handle exception
           }finally{
               c.close();
           }
      }
       		 

		

}
}
&#13;
MainActivity:

public class MainActivity extends Activity {
	
	SQLiteDatabase db;
	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    
		
		db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
		db.execSQL("CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, timestamp TEXT, mesg TEXT);");
		
	} //oncreate
	
	public void ViewAll(View v){
		try{
		Cursor c=db.rawQuery("SELECT * FROM student", null);
		if(c.getCount()==0)
		{
			showMessage("Error", "No records found");
			return;
		}
		StringBuffer buffer=new StringBuffer();
		while(c.moveToNext())
		{
			buffer.append("Sender: "+c.getString(1)+"\n");
			buffer.append("TimeStamp: "+c.getString(2)+"\n");
			buffer.append("Mesg: "+c.getString(3)+"\n\n");
		}
		showMessage("Student Details", buffer.toString());
		}
		catch(Exception ex)
		{
			System.out.println(ex.toString());
		}
	}
	
	
	public void Delete(View v){
		
		Cursor c=db.rawQuery("SELECT * FROM student WHERE id=(SELECT MIN(id) FROM student)", null);
		if(c.moveToFirst())
		{
			db.execSQL("DELETE FROM student WHERE id=(SELECT MIN(id) FROM student)");
			showMessage("Success", "Record Deleted");
		}
		else
		{
			showMessage("Error", "Invalid Entry");
		}
	}

	private void showMessage(String title, String message) {
		// TODO Auto-generated method stub
		Builder builder=new Builder(this);
    	builder.setCancelable(true);
    	builder.setTitle(title);
    	builder.setMessage(message);
    	builder.show();
	}
	


} //activty
&#13;
&#13;
&#13;

0 个答案:

没有答案