列名是否可以在SQLite中编号?

时间:2017-04-30 21:13:44

标签: android sqlite

我正在开发一个android应用程序,并使用了一个包含70行和100列的外部数据库。对于每一行,当我的应用用户点击下一个按钮时,该应用会转到数据库的下一列并显示其内容。所以我必须使用1到100的数字作为我的列的名称,通过增加或减少它的数字来引用所选列。但它在日食中出现错误。例如在ID = 1时,当我选择" mani"专栏,它就像一个魅力,但没有为#34; 1"列。

Database Class :
package com.codegostarNiloo.negar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database extends SQLiteOpenHelper {

public static String   DB_PATH;
public static String   DB_NAME = "negar.db";
private SQLiteDatabase myDataBase;
private final Context  myContext;



public Database(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}



public void createDataBase() throws IOException {

    copyDataBase();
    Log.d("isexist", "" + checkDataBase());
}



private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    }
    catch (SQLiteException e) {}

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}



private void copyDataBase() throws IOException {
    File dir = new File(DB_PATH);
    if ( !dir.exists()) {
        dir.mkdirs();
    }
    File database = new File(dir, DB_NAME);
    if ( !database.exists()) {

        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
}



public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}



@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}



@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

 }
}

MainActivity类:

// database method :
    private void Database() throws Error {
    myDbHelper = new Database(MainActivity.this);
    try {
        myDbHelper.createDataBase();
    }
    catch (IOException ioe) {

        throw new Error("Unable to create database");
    }

    try {
        myDbHelper.openDataBase();
    }
    catch (SQLException sqle) {
        throw sqle;
    }
}

// Select column 1 of row 1:
database();
db = myDbHelper.getWritableDatabase();
Cursor c = db.rawQuery("select * from mohtava where ID = " + 1, null);
    if (c.moveToNext()) {
    String j=c.getString(c.getColumnIndex("1"));
        } 
db.close();
c.close();

数据库:http://www.axgig.com/images/23013560911355303754.jpg

错误:

05-02 02:36:18.187: E/CursorWindow(26741): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 3 columns.
05-02 02:36:18.188: D/AndroidRuntime(26741): Shutting down VM
05-02 02:36:18.188: W/dalvikvm(26741): threadid=1: thread exiting with uncaught exception (group=0x410e39a8)
05-02 02:36:18.190: E/AndroidRuntime(26741): FATAL EXCEPTION: main
05-02 02:36:18.190: E/AndroidRuntime(26741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codegostarNiloo.negar/com.codegostarNiloo.negar.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2377)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread.access$600(ActivityThread.java:167)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.os.Looper.loop(Looper.java:153)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread.main(ActivityThread.java:5341)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at java.lang.reflect.Method.invoke(Method.java:511)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at dalvik.system.NativeStart.main(Native Method)
05-02 02:36:18.190: E/AndroidRuntime(26741): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.database.CursorWindow.nativeGetString(Native Method)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.database.CursorWindow.getString(CursorWindow.java:434)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at com.codegostarNiloo.negar.MainActivity.amoozesh(MainActivity.java:251)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at com.codegostarNiloo.negar.MainActivity.onCreate(MainActivity.java:114)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.Activity.performCreate(Activity.java:5125)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-02 02:36:18.190: E/AndroidRuntime(26741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2289)
05-02 02:36:18.190: E/AndroidRuntime(26741):    ... 11 more

1 个答案:

答案 0 :(得分:0)

好的,我的问题已经解决了。我可以从具有数字名称的列中读取记录!

我已经使用NOX模拟器和我的设计测试这个应用程序,他们每次都缓存数据库并参考它!

所以,只需卸载NOX并在我的设计中删除以前安装的应用程序然后重新安装...... 一切都像魅力一样!