rawQuery的使用

File databaseFile = getDatabasePath(Contents.EnMicroMsg_sql_test);
					Log.d("wwwww", databaseFile.toString());
					SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(databaseFile, uin, null, hook);
					
					
					String userinfo = "{"+"\"userInfo\":" + "{";; 
		    		Cursor c3 = db.rawQuery("select * from userinfo", null);
		    		while (c3.moveToNext()) {
		    			String id = c3.getString(c3.getColumnIndex("id"));
		    			String value = c3.getString(c3.getColumnIndex("value"));
		    			//System.out.println("id="+id);
		    			if(id.equals("2")){
		    				wechat_number = value;
		    				Log.i("wechat_number", wechat_number);
		    			}
		    			if(id.equals("42")){
		    				wechat_number = value;
		    			}
		    			if(id.equals("3")){
		    				alias = value;
		    			}
		    			if(id.equals("4")){
		    				nickname = value;
		    			}
		    			if(id.equals("6")){
		    				mobile = value;
		    			}
		    		}
		    		
		    		userinfo = userinfo + "\"wechat_number\":\"" + wechat_number + "\",\"alias\":\"" + alias + "\",\"nickname\":\"" + nickname + "\",\"phone\":\"" + mobile +"\"},";
					
					
					
					
					
					jsonSql_message = "{" + "\"message\":" + "[";
					jsonSql_rcontact = userinfo + "\"rcontact\":" + "[";
					// Cursor c = db.query("message", null, null, null, null, null, null);
					//db.query("message", null, null, null, null, null, "time", 2);
					//Cursor c = db.rawQuery("select * from message order by createTime desc limit 3", null);
					//Cursor c = db.rawQuery("select * from message order by msgId desc limit 50", null);
					Cursor c = db.rawQuery("select * from message where createTime>? order by msgId limit 2", new String[]{lastTime});
					while (c.moveToNext()) {
						int id = c.getInt(c.getColumnIndex("msgId"));
						String chat_type = c.getString(c.getColumnIndex("type"));
						String isSend = c.getString(c.getColumnIndex("isSend"));
						String createTime = c.getString(c.getColumnIndex("createTime"));
						lastTime = createTime;
						String content = c.getString(c.getColumnIndex("content"));
						jsonSql_message = jsonSql_message + "{\"chat_type\":\"" + chat_type + "\",\"content\":\"" + content + "\",\"chat_from\":\"" + "小明" + "\",\"chat_to\":\"" + "小刚" + "\",\"datetime\":\"" + createTime + "\",\"imgpath\":\"" + "http://66" + "\",\"issend\":\"" + isSend + "\"},";
					}
					if(jsonSql_message.equals("{" + "\"message\":" + "[")){
						jsonSql_message = "{" + "\"message\":" + "\"\"}";
					} else {
						jsonSql_message = jsonSql_message.substring(0, jsonSql_message.length()-1) + "]}";
					}
					Log.i("jsonSql_message", jsonSql_message);
					
					//Cursor c2 = db.rawQuery("select * from rcontact where alias=?", new String[]{"dyd15063176692"});
//					Cursor c2 = db.rawQuery("select * from rcontact limit 5", null);
					Cursor c2 = db.rawQuery("select * from rcontact where type in(?,?,?,?)", new String[]{"0","33","2","4"});
					
					
//					c2.moveToFirst();
//					while (!c2.isAfterLast()) {
//						String username = c2.getString(c2.getColumnIndex("username"));
//						String alias = c2.getString(c2.getColumnIndex("alias"));
//						String conRemark = c2.getString(c2.getColumnIndex("conRemark"));
//						String wechatfriend_number;
//						if(alias.equals("")){
//							wechatfriend_number = username;
//						} else {
//							wechatfriend_number = alias;
//						}
//						//Log.i("db", "username=>" + username);
//						//jsonSql_rcontact = jsonSql_rcontact + "{\"buddyname\":\"" + conRemark + "\",\"imgid\":\"" + "1" + "\",\"wxname\":\"" + nickname + "\",\"province\":\"" + "北京" + "\",\"city\":\"" + "昌平" + "\",\"pyInitial\":\"" + pyInitial + "\",\"pyQuanPin\":\"" + pyQuanPin + "\",\"lastchattime\":\"" + currenttime + "\",\"wechatfriend_number\":\"" + wechatfriend_number + "\",\"iskeepUser\":" + "1" + ",\"buddyphotourl\":\"" + "localhost:8080/weroot/0004.png" + "\",\"dr\":\"" + "0" + "\",\"sex\":\"" + "1" + "\",\"initwechatfriend_number\":\"" + "111111111" + "\",\"show_head\":\"" + "1" + "\"},";
//						jsonSql_rcontact = jsonSql_rcontact + "{\"buddyname\":\"" + conRemark + "\",\"show_head\":\"" + "1" + "\"},";
//					    c2.moveToNext();
//					}

					
					while (c2.moveToNext()) {
						String username = c2.getString(c2.getColumnIndex("username"));
						String alias = c2.getString(c2.getColumnIndex("alias"));
						String conRemark = c2.getString(c2.getColumnIndex("conRemark"));
						String nickname = c2.getString(c2.getColumnIndex("nickname"));
						String pyInitial = c2.getString(c2.getColumnIndex("pyInitial"));
						String type = c2.getString(c2.getColumnIndex("type"));
						String pyQuanPin = c2.getString(c2.getColumnIndex("quanPin"));
						String wechatfriend_number;
						if(alias.equals("")){
							wechatfriend_number = username;
						} else {
							wechatfriend_number = alias;
						}
						//Log.i("db", "username=>" + username);
						jsonSql_rcontact = jsonSql_rcontact + "{\"buddyname\":\"" + conRemark + "\",\"imgid\":\"" + "1" + "\",\"wxname\":\"" + nickname + "\",\"province\":\"" + "北京" + "\",\"city\":\"" + "昌平" + "\",\"pyInitial\":\"" + pyInitial + "\",\"pyQuanPin\":\"" + pyQuanPin + "\",\"lastchattime\":\"" + currenttime + "\",\"wechatfriend_number\":\"" + wechatfriend_number + "\",\"iskeepUser\":" + "1" + ",\"buddyphotourl\":\"" + "localhost:8080/weroot/0004.png" + "\",\"dr\":\"" + "0" + "\",\"sex\":\"" + "1" + "\",\"initwechatfriend_number\":\"" + "111111111" + "\",\"show_head\":\"" + "1" + "\"},";
						//jsonSql_rcontact = jsonSql_rcontact + "{\"buddyname\":\"" + conRemark + "\",\"show_head\":\"" + "1" + "\"},";
					}
					
					jsonSql_rcontact = jsonSql_rcontact.substring(0, jsonSql_rcontact.length()-1) + "]}";
					
					c2.close();
					c3.close();
					c.close();
					db.close();
					
//					SendUserMessage(jsonSql_message);
					SendUserRcontact(jsonSql_rcontact);

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/bsmmaoshenbo/article/details/52317652
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞