Android数据库;解决最后几个错误
我在下面的代码中将两个类合并为一个。我接近成功,但仍有一些错误。我的编程环境告诉我在以下方面存在错误: 第51行-未为类型SQLiteDatabase定义open()方法 第56行-未为类型SQLiteDatabase定义open()方法 第57行-类型SQLiteDatabase的getAllEntries()方法未定义 第98行-无法从类型SQLiteOpenHelper对非静态方法getWritableDatabase()进行静态引用 第99行-Void方法无法返回值(此处是否需要返回任何内容?) 第102行-无法从类型SQLiteOpenHelper对非静态方法close()进行静态引用 我尝试过重新排列代码,以防出现问题。我不确定它说“类型[type]的方法未定义”是什么意思。使用Google,我无法确定这些错误消息的含义 任何帮助都将不胜感激Android数据库;解决最后几个错误,android,database,syntax-error,Android,Database,Syntax Error,我在下面的代码中将两个类合并为一个。我接近成功,但仍有一些错误。我的编程环境告诉我在以下方面存在错误: 第51行-未为类型SQLiteDatabase定义open()方法 第56行-未为类型SQLiteDatabase定义open()方法 第57行-类型SQLiteDatabase的getAllEntries()方法未定义 第98行-无法从类型SQLiteOpenHelper对非静态方法getWritableDatabase()进行静态引用 第99行-Void方法无法返回值(此处是否需要返回任何
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;
}
}
在oncreateremove
dbHelper=newdbhelper中(this)
并添加以下行
DBAdapter(this);
open();
及
您应该将数据库代码转换为类。在onCreate中进行更改后,我收到以下错误:DBAdapter无法解析为类型。在对open方法进行更改后,我收到了相同的错误。谢谢你的帮助,其他人都成功了。最新的编辑修复了这些行,谢谢。对于第51行,我猜我将db.open()更改为open()?是的,您只需更改一些内容,例如在open()方法中返回DBAdapter等等。我从第56行和第57行中删除了“db.”,错误就消失了。希望它能起到一定的作用。您必须删除所有数据库。在代码中,没有db.close(),只需close()。您可以在onCreate中删除open()。
public void open(){
db=dbHelper.getWritableDatabase(); //line 98
}
public void close(){
dbHelper.close(); //line 102
}