有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从SQLite插入和获取真实数据类型会为连续插入获取空值吗?

我一直在想,当我在SOP(sys.out.println)上的同一个db/表中插入相同的数据(类型为Double的数组)时,为什么会出现“null(s)”,因为我没有设置任何约束

我怀疑“空”会进入我的表格,但这怎么可能呢?我第一次插入值时,它工作正常,但一旦我再次尝试插入相同的值(第二次运行我的应用程序),我会在logcat上获得SOP的输出,如下所示:

12-14 13:24:43.788: I/System.out(763): 28.0
12-14 13:24:43.788: I/System.out(763): null
12-14 13:24:43.797: I/System.out(763): The data was added successfully!
12-14 13:24:43.797: I/System.out(763): Data Totals = 2

我的数据库适配器。java看起来像这样:

package com.profourth.line;

import java.util.Random;

import 安卓.content.ContentValues;
import 安卓.content.Context;
import 安卓.database.Cursor;
import 安卓.database.SQLException;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;
import 安卓.util.Log;

public class DBAdapter {
    int id = 0;
    public static final String KEY_PHID = "phID";
    public String KEY_LAT = "LATS";
    public String KEY_LON = "LONS";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "PROdb";
    private static final String DATABASE_TABLE = "myPROmapData";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE = "create table myPROmapData (phID integer, LATS real, LONS real);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS myPROmapData");
            onCreate(db);
        }
    }

    // ---opens the database---
    public DBAdapter open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    // ---closes the database---
    public void close() {
        DBHelper.close();
    }

    // ---insert a title into the database---
    public long insertQuote(/* String Quote */) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PHID, 100);
        initialValues.put(KEY_LAT, 28.00);
        initialValues.put(KEY_LON, 72.00);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public int getAllEntries() {
        Cursor cursor = db.rawQuery("SELECT COUNT(LATS) FROM myPROmapData",
                null);
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        }
        return cursor.getInt(0);

    }

    public Double[] Populate() {
        Double[] myLats;
        Double[] myLons;
        id = getAllEntries();
        /*
         * Random random = new Random(); int rand =
         * random.nextInt(getAllEntries()); if(rand == 0) ++rand;
         */
        Cursor cursor = db.rawQuery("SELECT LATS FROM myPROmapData", null);
        /*
         * if(cursor.moveToFirst()) { //return cursor.getString(0); } //return
         * cursor.getString(0); return myLats;
         */

        Double data[] = new Double[cursor.getCount()];

        if (cursor != null) {

            while (cursor.moveToNext()) {
                int j = 0;
                while (j < cursor.getColumnCount()) {
                    // data[j] = cursor.getString(j);
                    data[j] = cursor.getDouble(j);
                    j++;
                }

                cursor.moveToNext();
            }
            cursor.close();
        }
        return data;

    }

}

我的活动看起来像:

package com.profourth.line;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.view.Menu;
import 安卓.widget.Toast;

public class MapDataController extends Activity {

    DBAdapter db = new DBAdapter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_data_controller);

        /*MapDataHandler dbh = new MapDataHandler(this);                                       
        SQLiteDatabase db = dbh.getWritableDatabase(); 

        MapDataHandler handlemydata = new MapDataHandler(null);
        handlemydata.onCreate(db);
        handlemydata.AddRows();
        handlemydata.PopulateArr();
        db.close();*/

        db.open();
        long id = 0;
        db.insertQuote();
        id = db.getAllEntries();

        Double[] dta = null;
        dta = db.Populate();
        for(int z=0;z<(dta.length);z++)
        {
            System.out.println(dta[z]);
        }

        //Context context = getApplicationContext();
        CharSequence text = "The data was added successfully!\nData Totals = " + id;
        /*int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();*/
        System.out.println(text);
        //Quote.setText("");
        db.close();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_map_data_controller, menu);
        return true;
    }

}

非常感谢您的帮助。谢谢你的阅读

解决: 下面是我更新的Populate()方法:

public Double[] Populate() {
        Double[] myLats;
        Double[] myLons;
        //Double[] data = null;

        id = getAllEntries();
        /*
         * Random random = new Random(); int rand =
         * random.nextInt(getAllEntries()); if(rand == 0) ++rand;
         */
        int columnIndex = 0;
        Cursor cursor = db.rawQuery("SELECT LATS FROM myPROmapData", null);
        /*
         * if(cursor.moveToFirst()) { //return cursor.getString(0); } //return
         * cursor.getString(0); return myLats;
         */

        Double data[] = new Double[cursor.getCount()];
        if (cursor.moveToFirst())
        {                       
            for (int i = 0; i < cursor.getCount(); i++)
            {
                data[i] = cursor.getDouble(columnIndex);
                cursor.moveToNext();
            }           
        }
        cursor.close();
return data;
}

共 (1) 个答案

  1. # 1 楼答案

    代码在循环中有两个moveToNext()调用,因此它每秒读取一次值,并将数组的最后一半留空