Android数据库;解决最后几个错误

时间:2013-04-11 23:04:08

标签: android database syntax-error

我已经接受了两个类,并在下面的代码中将它们合并为一个。我接近成功,但仍然存在一些错误。我的编程环境告诉我有错误:

第51行 - 对于SQLiteDatabase类型

,未定义open()方法

第56行 - 对于SQLiteDatabase类型

,未定义open()方法

第57行 - 方法getAllEntries()未定义类型SQLiteDatabase

第98行 - 无法从SQLiteOpenHelper类型中对非静态方法getWritableDatabase()进行静态引用

第99行 - Void方法无法返回值(我需要在此处返回任何内容吗?)

第102行 - 无法对SQLiteOpenHelper类型的非静态方法close()进行静态引用

我已经尝试重新安排代码,以防出现问题。我不确定它是什么意思,“它的类型[类型]的方法是未定义的。”使用Google,我无法确定这些错误消息的含义。

非常感谢任何帮助。

package com.example.databaseProject;

import android.os.Bundle;
import android.app.Activity;
import android.content.*;
import android.content.DialogInterface.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.Context;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity {
    private SQLiteDatabase db;
    private DBHelper dbHelper;
    private EditText Quote;
    int id=0;
    public static final String KEY_ROWID="_id";
    public static final String KEY_QUOTE="Quote";
    private static final String TAG="DBAdapter";
    private static final String DATABASE_NAME="Random";
    private static final String DATABASE_TABLE="tblRandomQuotes";
    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_CREATE="create table tblRandomQuotes (_id integer primary key autoincrement, "+"Quote text not null );";
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //linkingButton
        Button saveButton=(Button)findViewById(R.id.button1);
        //add onclick listener to saveButton
        saveButton.setonclickListener(mAddListener);
        dbHelper=new DBHelper(this);

    }
    //Create an anonymous implementation of onclickListener
    private onclickListener mAddListener = new onclickListener(){
        public void onclick(View v){
            switch(v.getId()){
                case R.id.button1:
                    db.open();  //line 51
                    long id=0;
                    //do something when the button is clicked
                    try{
                        Quote=(EditText)findViewById(R.id.lastName1);
                        db.insertQuote(Quote.getText().toString());  //line 56
                        id=db.getAllEntries();  //line 57
                        Context context=getApplicationContext();
                        CharSequence text="The quote '"+Quote.getText()+"' was added successfully!\nQuotes Total = "+id;
                        int duration=Toast.LENGTH_LONG;
                        Toast toast=Toast.makeText(context, text, duration);
                        toast.show();
                        Quote.setText("");
                    }
                    catch(Exception ex){
                        Context context=getApplicationContext();
                        CharSequence text=ex.toString()+"ID = "+id;
                        int duration=Toast.LENGTH_LONG;
                        Toast toast=Toast.makeText(context, text, duration);
                        toast.show();
                    }
                    db.close();
                    //break;
               }
           }
    };

    public void DBAdapter(Context ctx){//I think this might go up in the constructor
        this.context=ctx;
        dbHelper=new DBHelper(context);
    }
    private class DBHelper extends SQLiteOpenHelper{
        public DBHelper(Context c){
            super(c,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 tblRandomQuotes");
            onCreate(db);
        }
    }
    public void open(){
        db=DBHelper.getWritableDatabase();  //line 98
        return this;  //line 99
    }
    public void close(){
        DBHelper.close();  //line 102
    }
    public long insertQuote(String Quote){
        ContentValues initialValues=new ContentValues();
        initialValues.put(KEY_QUOTE,Quote);
        return db.insert(DATABASE_TABLE,null,initialValues);
    }
    public int getAllEntries(){
        Cursor cursor=db.rawQuery("SELECT COUNT(Quote) FROM tblRandomQuotes", null);
        if(cursor.moveToFirst()){
            return cursor.getInt(0);
        }
        return cursor.getInt(0);
    }

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

}

1 个答案:

答案 0 :(得分:1)

在onCreate中删除dbHelper=new DBHelper(this);并添加以下行

DBAdapter(this);

open();  

并且

public void open(){
    db=dbHelper.getWritableDatabase();  //line 98
}

public void close(){
    dbHelper.close();  //line 102
}  

您应该将数据库代码变成一个类。