打印数据库到字符串不起作用

时间:2017-01-24 18:43:49

标签: android sql database

免责声明 我是Android新手,我正在关注教程

我正在尝试将数据库打印到方法 databaseToString

MyDbHandler 文件中的字符串 dbString

它无效,因为dbString保持为空。

我还尝试删除while循环并仅使用此代码:

dbString += c.getString(c.getColumnIndex("productname"));

给出了以下运行时错误:

FATAL EXCEPTION: main
                                                                              Process: com.example.jarno.a49_savingdata, PID: 10976
                                                                              java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                  at android.view.View.performClick(View.java:5637)
                                                                                  at android.view.View$PerformClick.run(View.java:22429)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                               Caused by: java.lang.reflect.InvocationTargetException
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                  at android.view.View.performClick(View.java:5637) 
                                                                                  at android.view.View$PerformClick.run(View.java:22429) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                               Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                                                  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                                  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                                  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                                  at com.example.jarno.a49_savingdata.MyDbHandler.databaseToString(MyDbHandler.java:65)
                                                                                  at com.example.jarno.a49_savingdata.MainActivity.printDatabase(MainActivity.java:45)
                                                                                  at com.example.jarno.a49_savingdata.MainActivity.addButtonClicked(MainActivity.java:35)
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                  at android.view.View.performClick(View.java:5637) 
                                                                                  at android.view.View$PerformClick.run(View.java:22429) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

查看以下代码,我假设产品未正确添加或打印方法无法正确读取数据库。

caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

MyDbHandler.java

package com.example.jarno.a49_savingdata;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


    public class MyDbHandler extends SQLiteOpenHelper {

    private static final String TAG = "debug";

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "products.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    public MyDbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT "  +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP_TABLE_IF_EXISTS" + TABLE_PRODUCTS);
        onCreate(db);
    }

    public void addProduct(Products product) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    public void delProduct(String productName) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";" );
    }

    public String databaseToString() {
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
        // Cursor point to a location in the results
        Cursor c = db.rawQuery(query, null);
        // Move to the first row in the results
        c.moveToFirst();



       while(!c.isAfterLast()) {
            if(c.getString(c.getColumnIndex("productname")) != null) {
                dbString += c.getString(c.getColumnIndex("productname"));
                Log.i(TAG, "loop");
                dbString += "\n";
                c.moveToNext();
            }
        }

        Log.i(TAG, dbString);
        db.close();
        return dbString;
    }
}

MainActivity.java

package com.example.jarno.a49_savingdata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "debug";

    EditText jarnoInput;
    TextView jarnoText;
    MyDbHandler dbHandler;

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

        jarnoInput = (EditText) findViewById(R.id.jarnoInput);
        jarnoText = (TextView) findViewById(R.id.jarnoText);

        dbHandler = new MyDbHandler(this, null, null, 1);
        //printDatabase();

    }

    public void addButtonClicked(View v) {
        Products product = new Products(jarnoInput.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

    public void delButtonClicked(View v) {
        String inputText = jarnoInput.getText().toString();
        dbHandler.delProduct(inputText);
        printDatabase();
    }

    public void printDatabase() {
        String dbString = dbHandler.databaseToString();
        jarnoText.setText(dbString);
        jarnoInput.setText("");
    }
}

Products.java

package com.example.jarno.a49_savingdata;



public class Products {

    private int _id;
    private String _productname;

    public Products() {
    }

    public Products(String productname) {
        this._productname = productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }
}

EDIT1

根据对此的回答更改了代码:

Cursor c = db.query(
                TABLE_PRODUCTS,
                new String[] { "productname" },
                "1",
                new String[] { "id" },
                null,
                null,
                null
        );

        // Move to the first row in the results
        c.moveToFirst();

        if (c.getCount() >= 1) {
            while(c.moveToNext()) {
                if(c.getString(c.getColumnIndex("productname")) != null) {
                    dbString += c.getString(c.getColumnIndex("productname"));
                    Log.i(TAG, "loop");
                    dbString += "\n";
                }
            }
        }

收到以下错误:

FATAL EXCEPTION: main
                                                                                Process: com.example.jarno.a49_savingdata, PID: 9807
                                                                                java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                    at android.view.View.performClick(View.java:5637)
                                                                                    at android.view.View$PerformClick.run(View.java:22429)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                    at android.view.View.performClick(View.java:5637) 
                                                                                    at android.view.View$PerformClick.run(View.java:22429) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                                 Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
                                                                                    at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
                                                                                    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
                                                                                    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
                                                                                    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                                    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                                    at com.example.jarno.a49_savingdata.MyDbHandler.databaseToString(MyDbHandler.java:61)
                                                                                    at com.example.jarno.a49_savingdata.MainActivity.printDatabase(MainActivity.java:45)
                                                                                    at com.example.jarno.a49_savingdata.MainActivity.addButtonClicked(MainActivity.java:35)
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                    at android.view.View.performClick(View.java:5637) 
                                                                                    at android.view.View$PerformClick.run(View.java:22429) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

2 个答案:

答案 0 :(得分:0)

光标从-1开始,将你的循环修改为:

if(cursor.getCount() >= 1) {
  while(cursor.moveToNext()) {
    // Access cursor values here
  }
}

我建议不要使用rawQuery,像这样查询数据库:https://stackoverflow.com/a/5276236/5063104

编辑(光标代码)

请修改您的选择参数,如下所示:https://stackoverflow.com/a/20777605/5063104

答案 1 :(得分:0)

dbString += c.getString(c.getColumnIndex("productname"));

如果要将所有字符串追加到一个使用StringBuilders或StringBufffers.others获取字符串,而游标!= null并将它们放在数组中

,这是获取所有字符串的正确方法