0

有人可以帮我写这个应用程序,但它不起作用,我找不到解决方案。

第一个错误:

java.lang.illegalstateexception 无法从光标窗口读取第 0 行第 1 行。确保游标在从中访问数据之前已正确初始化

第二个错误:

android.database.sqlite.SQLite 异常:靠近“=”:语法错误(代码 1):编译时:SELECT ZAHL1 FROM Verlauf WHERE ID =

package com.example.to_doliste2;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivityT extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t);
        datasource = new VerlaufDataSource(this);

    }

    private VerlaufDataSource datasource;

    List<Entry> AufgabenListe = new ArrayList<Entry>();

    public void VerlaufKlick(View view)
    {
        try
        {
        String zahl1;
        EditText Feld1 = (EditText)findViewById(R.id.editText1);

        if (Feld1.getText().toString().length() == 0)
                {
            return;
                }

        zahl1 = (Feld1.getText().toString());

        Feld1.setText(String.valueOf(zahl1));




        try
        {
            datasource.open();
            datasource.createEntry(zahl1);
            datasource.close();         
        }
        catch (Exception ex)
        {
            Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
        }



        AufgabenListe.clear();
        try
        {
            datasource.open();
            AufgabenListe = datasource.getAllEntries();
            datasource.close();
        }
        catch (Exception ex)
        {
            Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
        }


        ArrayAdapter<Entry> adapterVerlauf = new ArrayAdapter<Entry>(MainActivityT.this, android.R.layout.simple_list_item_1, AufgabenListe);

        ListView lVerlauf = (ListView)findViewById(R.id.listView1);
        lVerlauf.setAdapter(adapterVerlauf);
        }
        catch (Exception ex)
        {
            Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
        }

    }



}

package com.example.to_doliste2;

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


    public class MySQLHelper extends SQLiteOpenHelper {

        public static final String DATABASE_NAME = "verlaufAufgaben.db";
        public static final int DATABASE_VERSION = 1;

        private static final String TABLE_CREATE_VERLAUF = ""
                +"create table VERLAUF ("
                +" ID integer primary key, "
                +" Zahl1 int) ";

        public MySQLHelper(Context context) 
        {   
            super (context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase database)
        {
            database.execSQL(TABLE_CREATE_VERLAUF);
        }


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

    }

package com.example.to_doliste2;

public class Entry {
    private int zahl1;

    public int getZahl1()
    {
        return zahl1;
    }

    public void setZahl1(int zahl1)
    {
        this.zahl1 = zahl1;
    }


    @Override
    public String toString()
    {
        return String.format("%d", zahl1);
    }

}

package com.example.to_doliste2;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class VerlaufDataSource {

    private SQLiteDatabase database;
    private MySQLHelper dbHelper;
    private String[] allColumns = {"ZAHL1"};


    public VerlaufDataSource(Context context)
    {
        dbHelper = new MySQLHelper(context);
    }

    public void open() throws SQLException
    {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    public Entry createEntry(String zahl1)
    {
        ContentValues values = new ContentValues();
        values.put("ZAHL1", zahl1);

        long insertId = database.insert("Verlauf", null, values);

        Cursor cursor = database.query("Verlauf", allColumns, "ID = " + insertId, null, null, null, null);
        cursor.moveToFirst();

        return cursorToEntry(cursor);



    }

    protected List<Entry> getAllEntries()
    {
        List<Entry> EntriesList = new ArrayList<Entry>();
        EntriesList = new ArrayList<Entry>();

        Cursor cursor = database.query("Verlauf", allColumns, "ID =" , null, null, null, null, null);
        cursor.moveToFirst();

        if(cursor.getCount() == 0) return EntriesList;

        while (cursor.isAfterLast()==false)
        {
            Entry entry = cursorToEntry(cursor);
            EntriesList.add(entry);
            cursor.moveToNext();
        }

        cursor.close();

        return EntriesList;
    }

    private Entry cursorToEntry(Cursor cursor)
    {
        Entry entry = new Entry();
        entry.setZahl1(cursor.getInt(1));

        return entry;
    }
}
4

2 回答 2

0

如果您要从数据库中检索所有列,则将查询更改为:

 Cursor cursor = database.query("Verlauf", 
                           allColumns, null, null, null, null, null);
于 2012-12-26T13:52:14.283 回答
0

首先,把cursor.getInt(1)里面VerlaufDataSource.cursorToEntry(Cursor)改成cursor.getInt(0),因为计数是从0开始的,而你只查询了一列(allColumns == { "ZAHL1" })。

其次,正如上面提到的 ρяσѕρєя K,"ID ="在 中不是正确的 WHERE 子句VerlaufDataSource.getAllEntries()null如果您想要所有列,请将其更改为。

于 2012-12-27T14:21:40.013 回答