从表

时间:2015-12-03 23:05:41

标签: android mysql sqlite

我从表中读取一行时收到错误。我的应用程序是这样的:

  • 在主要活动中选择要显示的表格
  • 然后另一个活动将开始显示数据库中的每个条目
  • 当您单击列表视图中的项目时,另一个活动将开始加载编辑文本中的选定行列 - 这就是错误发生的位置。

    public class DVD_activity extends AppCompatActivity {
    String a;
    String b;
    DB_Worker dw;
    int x;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dvd_activity);
        NapojAdapter();
        pridajListener();
    }
    
    private void NapojAdapter() {
        dw = new DB_Worker(this);
        SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,R.layout.dvd_list_layout,dw.VratKurzorD(),new String[] { DVD.KEY_ID,
                DVD.KEY_title,DVD.KEY_autor},new int[] {R.id.id2, R.id.nazov, R.id.autor},0);
    
        ListView lv = (ListView)findViewById(R.id.listView2);
        lv.setAdapter(myAdapter);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_dvd_activity, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == R.id.action_settings) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    public void button4click(View view) {
        Intent intent = new Intent (this, Pridaj_DVD.class);
        String verzia = "1";
        intent.putExtra("verzia",verzia);
        startActivityForResult(intent, 1);
    }
    
    private void pridajListener() {
        AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {public void onItemClick(AdapterView parent, View v, int position, long id) {
    
            TextView lv = (TextView)v.findViewById(R.id.id2);
            x = Integer.parseInt(lv.getText().toString());
            DVD s = dw.getDVD(x);
            a =s.title;
            b = s.autor;
            String verzia = "2";
            Intent intent = new Intent(DVD_activity.this,Pridaj_DVD.class);
            intent.putExtra("verzia", verzia);
            intent.putExtra("a",a);
            intent.putExtra("b",b);
            startActivityForResult(intent, 1);
    
        }};
        ListView myList= (ListView) findViewById(R.id.listView2);
        myList.setOnItemClickListener(mMessageClickedHandler);
    
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == 0) {
                DVD s = new DVD();
                s.title = data.getStringExtra("a");
                s.autor = data.getStringExtra("b");
                dw.addDVD(s);
                NapojAdapter();
                }
            if (resultCode == 1) {
                dw.deleteDVD(x);
                NapojAdapter();
            }
            if (resultCode == 2) {
                a = data.getStringExtra("a");
                b = data.getStringExtra("b");
                dw.updateDVD(Integer.toString(x),a,b);
                NapojAdapter();
            }
    
        }
    }
    }
    

这是DB_WORKER类:

public class DB_Worker {
public SQLiteDatabase db;
private DB_Helper dbHelper;

public DB_Worker(Context context) {
    dbHelper = new DB_Helper(context);
    db = dbHelper.getWritableDatabase();
}

public void addDVD(DVD s) {

    ContentValues values = new ContentValues();
    values.put(DVD.KEY_title, s.title);
    values.put(DVD.KEY_autor, s.autor);

    long newRowId = db.insert(DVD.TABLE,null,values);
}   

public DVD getDVD(long ID) {
    DVD s = new DVD();

    VratKurzorD();
    String[] projection = {DVD.KEY_title, DVD.KEY_autor};
    String selection = DVD.KEY_ID;
    String[] selectionArgs = {""+ID};
    Cursor c = db.query(DVD.TABLE,projection,selection,selectionArgs,null,null,null);

    s.DVD_ID = ID;
    s.title = c.getString(c.getColumnIndex(DVD.KEY_title));
    s.autor = c.getString(c.getColumnIndex(DVD.KEY_autor));

    c.close();
    return s;
}   

public void updateDVD(String ID, String title, String autor)
{
    ContentValues cv = new ContentValues();
    cv.put(DVD.KEY_title,title);
    cv.put(DVD.KEY_autor, autor);
    db.update(DVD.TABLE, cv, DVD.KEY_ID + "= ?", new String[]{ID});
}

public void deleteDVD(int ID) {
    db.delete(DVD.TABLE, DVD.KEY_ID + "= ?", new String[]{"" + ID});
}

public Cursor VratKurzorD() {
    Cursor c = db.rawQuery("select * from "+DVD.TABLE, null);
    c.moveToFirst();
    return c;
}

}

这是错误:

12-03 23:11:42.240 2067-2067/com.example.lowrider.pozicovna E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.lowrider.pozicovna, PID: 2067 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:1316)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
        at com.example.lowrider.pozicovna.DB_Worker.getDVD(DB_Worker.java:78)
        at com.example.lowrider.pozicovna.DVD_activity$1.onItemClick(DVD_activity.java:66)
        at android.widget.AdapterView.performItemClick(AdapterView.java:310)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
        at android.widget.AbsListView$3.run(AbsListView.java:3879)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

2 个答案:

答案 0 :(得分:0)

来自日志的信息:Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.表示您在某个地方撰写的查询信息如下: Select * from table并不需要任何参数,但是你提供了论证。 因此,您应该在代码中搜索没有?的查询,但是您将参数传递给函数。

答案 1 :(得分:0)

方法getDVD(long ID)更改

String selection = DVD.KEY_ID;

String selection = DVD.KEY_ID + "=?";

更新1:

此外,添加对moveToFirst()的调用以将光标定位到第一行:

Cursor c = db.query(DVD.TABLE,projection,selection,selectionArgs,null,null,null);
c.moveToFirst();

如果游标至少包含一行,这将有效。如果游标可能为空,则需要检查返回的布尔状态。