程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sqlite-Android關於數據庫的問題

sqlite-Android關於數據庫的問題

編輯:編程綜合問答
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) {

    }
    }

最佳回答:


試試把下面的代碼:

        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);
            }
helf19920825
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved