helf19920825
helinfengg
2015-06-04 09:30
采纳率: 50%
浏览 1.6k

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 11: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);
                }
    
    点赞 评论
  • caozhy

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

    点赞 评论
  • q3733353520

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

    点赞 评论
  • danielinbiti
    danielinbiti 2015-06-04 12:28

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

    点赞 评论

相关推荐