2 helf19920825 helf19920825 于 2015.06.04 17:30 提问

Android关于数据库的问题

谁帮忙看看,写入是成功了,但是获取的时候就获取不到数据啊!!!

package com.example.MySqlite;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

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

public class MyActivity extends Activity {
private ArrayList> list;
private DBHelper dbHelper;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dbHelper = new DBHelper(this);
    getData();
    inser();

    lookFor();
}

private void lookFor() {
    List<Map<String,String>> map = new ArrayList<Map<String, String>>();
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    Cursor cursor = database.query("notes",null,null,null,null,null,null);
    while(cursor!=null && cursor.getCount()>0){
        Map<String,String> has = new HashMap<String, String>();
        String name =cursor.getString(1);
        Log.d("NAME",name);
        String age = cursor.getString(2);
        String sex =cursor.getString(3);
        has.put("name",name);
        has.put("age",age);
        has.put("sex",sex);
        map.add(has);
    }

}

private void inser() {

   SQLiteDatabase sql=dbHelper.getWritableDatabase();

    for(int i=0;i<list.size();i++){
        ContentValues contentValues = new ContentValues();
        long id = i;
        String name = list.get(i).get("name");
        String age = list.get(i).get("age");
        String sex = list.get(i).get("sex");
        contentValues.put("id",id);
        contentValues.put("name",name);
        contentValues.put("age",age);
        contentValues.put("sex",sex);
        long hasSuccess = sql.insert("notes", null, contentValues);
    }

}

private void getData() {
    list = new ArrayList<Map<String, String>>();
    for(int i =0;i<5;i++){
        if(i==0){
            Map<String,String> map = new HashMap<String, String>();
            map.put("name","kity");
            map.put("age","25");
            map.put("sex","男");
            list.add(map);
        }else if(i==1) {
            Map<String,String> map = new HashMap<String, String>();
            map.put("name","Dave");
            map.put("age","21");
            map.put("sex","女");
            list.add(map);
        }else if(i==2) {
            Map<String,String> map = new HashMap<String, String>();
            map.put("name","Marces");
            map.put("age","26");
            map.put("sex","女");
            list.add(map);
        }else if(i==3) {
            Map<String,String> map = new HashMap<String, String>();
            map.put("name","Rose");
            map.put("age","12");
            map.put("sex","男");
            list.add(map);
        }else if(i==4) {
            Map<String,String> map = new HashMap<String, String>();
            map.put("name","LiLin");
            map.put("age","35");
            map.put("sex","女");
            list.add(map);
        }

    }
}

}

package com.example.MySqlite;

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

/**

  • Created by on 15-6-4.
    */
    public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context) {
    super(context,"mynote.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table if not exists notes(" +
    " id long key," +
    " name varchar(255)," +
    " age varchar(255)," +
    " sex varchar(255))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    }

4个回答

LogicTeamLeader
LogicTeamLeader   2015.06.04 19:58
已采纳

试试把下面的代码:

        String name =cursor.getString(1);
        Log.d("NAME",name);
        String age = cursor.getString(2);
        String sex =cursor.getString(3);

换成下面这个样子的

            int colums = cursor.getColumnCount();

            while (cursor.moveToNext()) {
                Map<String,String> map = new HashMap<String,String>();
                for (int i = 0; i < colums; i++) {
                    String col_name = cursor.getColumnName(i);
                    String col_value = cursor.getString(cursor
                            .getColumnIndex(col_name));
                    map.put(col_name, col_value);
                }
                listmap.add(map);
            }
LogicTeamLeader
LogicTeamLeader 回复_hay: 就是这一句:String col_value = cursor.getString(cursor .getColumnIndex(col_name));
2 年多之前 回复
helf19920825
helf19920825 感谢,换成你这个就查出来了!不清楚我错在那里了!大神告知一下!
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.04 17:54

在Cursor cursor = database.query("notes",null,null,null,null,null,null);下个断点调试下。

helf19920825
helf19920825 在Log的地方获取不到数据!打断点看过了!
2 年多之前 回复
q3733353520
q3733353520   2015.06.04 18:13

list = new ArrayList>();这个放到函数外

q3733353520
q3733353520 不好意思看错了,你可以在getdate() insert()的同时把list的数据也LOG看看 contentValues也是
2 年多之前 回复
danielinbiti
danielinbiti   Ds   Rxr 2015.06.04 20:28

把SQLite的数据库文件拷出来,用第三方可视化工具查询一下,看数据是否在库表中存在。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!