2 u012726878 u012726878 于 2015.06.11 17:32 提问

求解啊 android数据库问题

我在做一个android的app 用到了数据库 就在手机上创建数据库 结果显示的一直都是最后一次存入的信息 比如我存了三条 前面两个不是cmcc 最后一条存的是cmcc的信息 结果就显示了三条cmcc 由于手机没有root 也没有刷机 看不到数据库源文件 求解大神解答

8个回答

LogicTeamLeader
LogicTeamLeader   2015.06.11 22:06

呼呼,亏我看完了。

u014300915
u014300915   2015.06.11 17:37

你可以用模拟器啊,默认就是root了的

hikyson
hikyson   2015.06.11 17:56

代码贴出来,这绝对是代码没写对。

u011240877
u011240877   2015.06.11 18:07

恩,估计是你写错了~~~~~~~~~~~

u012726878
u012726878   2015.06.11 18:18

这是activity
package com.chongyou.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chongyou.dao.WifiDao;
import com.chongyou.db.WifiInfoSQLiteOpenHelper;
import com.chongyou.wifiAdmin.SerializableMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SaveActivity extends Activity implements OnClickListener {
public static int RESULT_CODE=2;
private TextView tv_save;
private Button but01;
private Button but02;
private Button but03;
private Map map;
List> data2=new ArrayList>();
private ListView lv_save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save);
    lv_save=(ListView) findViewById(R.id.lv_save);
    tv_save=(TextView) findViewById(R.id.tv_save);
    but01=(Button) findViewById(R.id.but01);
    but02=(Button) findViewById(R.id.but02);
    but03=(Button) findViewById(R.id.but03);
    but01.setOnClickListener(this);
    but02.setOnClickListener(this);
    but03.setOnClickListener(this);

    Bundle bundle = getIntent().getExtras();  
    SerializableMap serializableMap = (SerializableMap) bundle  
            .get("wifiInfo");  
    map = serializableMap.getMap();
    tv_save.setText(map.toString());
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
   case R.id.but01:
        WifiInfoSQLiteOpenHelper openHelper=new WifiInfoSQLiteOpenHelper(this);
        openHelper.getWritableDatabase();
        insert();
        break;
   case R.id.but02:
        queryAll();
        break;
   case R.id.but03:
        deleteAll();
        break;
    default:
        break;
    }
}


public void insert(){
    WifiDao dao=new WifiDao(getBaseContext());
    dao.insert(map);
    Toast.makeText(this, "存储成功", 0).show();
}
public void queryAll(){
    WifiDao dao=new WifiDao(getBaseContext());
    data2=dao.queryAll();
    SimpleAdapter adapter=new SimpleAdapter(this, 
            data2, 
            R.layout.savewifi_item, 
            new String[]{"wifiName","wifiStrength","MacAddress"}, 
            new int[]{R.id.wifi_name2,R.id.wifi_strength2,R.id.macaddress2});
    lv_save.setAdapter(adapter);
}

private void deleteAll() {
    WifiDao dao=new WifiDao(getBaseContext());
    dao.deleteAll();
    Toast.makeText(this, "删完喽", 0).show();
}

}

这是dao包

package com.chongyou.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chongyou.db.WifiInfoSQLiteOpenHelper;

public class WifiDao {
private WifiInfoSQLiteOpenHelper mOpenHelper;

public WifiDao(Context context){
    mOpenHelper = new WifiInfoSQLiteOpenHelper(context);
}
public void insert(Map<String, String> map){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        db.execSQL("insert into wifiInfo(wifiName,wifiStrength,MacAddress)values(?,?,?);",new Object[]{map.get("wifiName"),map.get("wifiStrength"),map.get("MacAddress")});
        db.close();
    }
}
public List<Map<String, String>> queryAll(){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        Cursor cursor=db.rawQuery("select * from wifiInfo;", null);
        if(cursor!=null&&cursor.getCount()>0){
            List<Map<String, String>> wifiQueryList=new ArrayList<Map<String,String>>();
            Map<String, String> wifiQuery=new HashMap<String, String>();
            while(cursor.moveToNext()){
                wifiQuery.put("_id", cursor.getInt(0)+"");
                wifiQuery.put("wifiName",cursor.getString(1));
                wifiQuery.put("wifiStrength",cursor.getString(2));
                wifiQuery.put("MacAddress",cursor.getString(3));
                wifiQueryList.add(wifiQuery);
            }
            db.close();
            return wifiQueryList;
        }
        db.close();
    }
    return null;
}

public void deleteAll(){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        db.execSQL("delete from wifiInfo;");
        db.close();
    }
}

}

u012726878
u012726878   2015.06.11 18:18

这是activity
package com.chongyou.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chongyou.dao.WifiDao;
import com.chongyou.db.WifiInfoSQLiteOpenHelper;
import com.chongyou.wifiAdmin.SerializableMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SaveActivity extends Activity implements OnClickListener {
public static int RESULT_CODE=2;
private TextView tv_save;
private Button but01;
private Button but02;
private Button but03;
private Map map;
List> data2=new ArrayList>();
private ListView lv_save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save);
    lv_save=(ListView) findViewById(R.id.lv_save);
    tv_save=(TextView) findViewById(R.id.tv_save);
    but01=(Button) findViewById(R.id.but01);
    but02=(Button) findViewById(R.id.but02);
    but03=(Button) findViewById(R.id.but03);
    but01.setOnClickListener(this);
    but02.setOnClickListener(this);
    but03.setOnClickListener(this);

    Bundle bundle = getIntent().getExtras();  
    SerializableMap serializableMap = (SerializableMap) bundle  
            .get("wifiInfo");  
    map = serializableMap.getMap();
    tv_save.setText(map.toString());
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
   case R.id.but01:
        WifiInfoSQLiteOpenHelper openHelper=new WifiInfoSQLiteOpenHelper(this);
        openHelper.getWritableDatabase();
        insert();
        break;
   case R.id.but02:
        queryAll();
        break;
   case R.id.but03:
        deleteAll();
        break;
    default:
        break;
    }
}


public void insert(){
    WifiDao dao=new WifiDao(getBaseContext());
    dao.insert(map);
    Toast.makeText(this, "存储成功", 0).show();
}
public void queryAll(){
    WifiDao dao=new WifiDao(getBaseContext());
    data2=dao.queryAll();
    SimpleAdapter adapter=new SimpleAdapter(this, 
            data2, 
            R.layout.savewifi_item, 
            new String[]{"wifiName","wifiStrength","MacAddress"}, 
            new int[]{R.id.wifi_name2,R.id.wifi_strength2,R.id.macaddress2});
    lv_save.setAdapter(adapter);
}

private void deleteAll() {
    WifiDao dao=new WifiDao(getBaseContext());
    dao.deleteAll();
    Toast.makeText(this, "删完喽", 0).show();
}

}

这是dao包

package com.chongyou.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chongyou.db.WifiInfoSQLiteOpenHelper;

public class WifiDao {
private WifiInfoSQLiteOpenHelper mOpenHelper;

public WifiDao(Context context){
    mOpenHelper = new WifiInfoSQLiteOpenHelper(context);
}
public void insert(Map<String, String> map){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        db.execSQL("insert into wifiInfo(wifiName,wifiStrength,MacAddress)values(?,?,?);",new Object[]{map.get("wifiName"),map.get("wifiStrength"),map.get("MacAddress")});
        db.close();
    }
}
public List<Map<String, String>> queryAll(){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        Cursor cursor=db.rawQuery("select * from wifiInfo;", null);
        if(cursor!=null&&cursor.getCount()>0){
            List<Map<String, String>> wifiQueryList=new ArrayList<Map<String,String>>();
            Map<String, String> wifiQuery=new HashMap<String, String>();
            while(cursor.moveToNext()){
                wifiQuery.put("_id", cursor.getInt(0)+"");
                wifiQuery.put("wifiName",cursor.getString(1));
                wifiQuery.put("wifiStrength",cursor.getString(2));
                wifiQuery.put("MacAddress",cursor.getString(3));
                wifiQueryList.add(wifiQuery);
            }
            db.close();
            return wifiQueryList;
        }
        db.close();
    }
    return null;
}

public void deleteAll(){
    SQLiteDatabase db=mOpenHelper.getWritableDatabase();
    if(db.isOpen()){
        db.execSQL("delete from wifiInfo;");
        db.close();
    }
}

}

u012726878
u012726878   2015.06.11 19:18

已经找到方法了
Map wifiQuery=new HashMap();
while(cursor.moveToNext()){
wifiQuery.put("_id", cursor.getInt(0)+"");
wifiQuery.put("wifiName",cursor.getString(1));
wifiQuery.put("wifiStrength",cursor.getString(2));
wifiQuery.put("MacAddress",cursor.getString(3));
wifiQueryList.add(wifiQuery);
}
应该是

        while(cursor.moveToNext()){
                    Map<String, String> wifiQuery=new HashMap<String, String>();
            wifiQuery.put("_id", cursor.getInt(0)+"");
            wifiQuery.put("wifiName",cursor.getString(1));
            wifiQuery.put("wifiStrength",cursor.getString(2));
            wifiQuery.put("MacAddress",cursor.getString(3));
            wifiQueryList.add(wifiQuery);
        }


                    存数据的临时map应该在while里面  谢谢各位的热心帮忙
vdsvfdsbfdgf
vdsvfdsbfdgf   2015.06.26 16:55

eclipse里面的ddms不是可以直接找到数据库导出来看

Csdn user default icon
上传中...
上传图片
插入图片