为什么Cursor cursor = database.rawQuery一直报错

Android 码拜 9年前 (2015-05-11) 1051次浏览 0个评论

package com.bignerdranch.android.english;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, TextWatcher
{
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ “/dictionary”;////获取外部存储的路径返回绝对路径的,其实就是你的SD卡的文件路径
private AutoCompleteTextView actvWord;
private final String DATABASE_FILENAME = “dictionary.db”;
private SQLiteDatabase database;
private Button btnSelectWord;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
database = openDatabase();
btnSelectWord = (Button) findViewById(R.id.search_btn);
actvWord = (AutoCompleteTextView) findViewById(R.id.search_word);
btnSelectWord.setOnClickListener(this);
actvWord.addTextChangedListener(this);
}

public class DictionaryAdapter extends CursorAdapter
{
private LayoutInflater layoutInflater;
@Override
public CharSequence convertToString(Cursor cursor)
{
return cursor == null ? “” : cursor.getString(cursor
.getColumnIndex(“_id”));
}

private void setView(View view, Cursor cursor)
{
TextView tvWordItem = (TextView) view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex(“_id”)));
}

@Override
public void bindView(View view, Context context, Cursor cursor)
{
setView(view, cursor);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View view = layoutInflater.inflate(R.layout.word_list_item, null);
setView(view, cursor);
return view;
}

public DictionaryAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}

public void afterTextChanged(Editable s)
{

        //  必须将english字段的别名设为_id 
Cursor cursor = database.rawQuery(“select english as _id from t_words where english like ?”,new String[]{ s.toString() + “%” });
DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor, true);
actvWord.setAdapter(dictionaryAdapter);

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub

}

public void onClick(View view)
{
String sql = “select chinese from t_words where english=?”;
Cursor cursor = database.rawQuery(sql, new String[]
{ actvWord.getText().toString() });
String result = “未找到该单词.”;
//  如果查找单词,显示其中文的意思
if (cursor.getCount() > 0)
{
//  必须使用moveToFirst方法将记录指针移动到第1条记录的位置
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex(“chinese”));
}
//  显示查询结果对话框
new AlertDialog.Builder(this).setTitle(“查询结果”).setMessage(result)
.setPositiveButton(“关闭”, null).show();

}

private SQLiteDatabase openDatabase()
{
try
{
// 获得dictionary.db文件的绝对路径
String databaseFilename = DATABASE_PATH + “/” + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary目录中存在,创建这个目录
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目录中不存在
// dictionary.db文件,则从res\raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
if (!(new File(databaseFilename)).exists())
{
// 获得封装dictionary.db文件的InputStream对象
InputStream is = getResources().openRawResource(
R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制dictionary.db文件
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}

fos.close();
is.close();
}
// 打开/sdcard/dictionary目录中的dictionary.db文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
}
catch (Exception e)
{
}
return null;
}

}

百度查了说4.0以前的版本db.close();会结束事务,而Jelly Bean 以后的版本因为安全性的问题,必须结束即endTransactiony以后才能再次访问本地数据库。但是加了还是没用,到游标那儿就一直报错!求帮助!

20分
建议你用SQLiteOpenHelper类来操作数据库
引用 1 楼 tongxin082 的回复:

建议你用SQLiteOpenHelper类来操作数据库

 
嗯 谢谢  但是  是那个游标的问题  我不懂为什么就不行  好像是现在高版本都不支持  不知道怎么改才会对

20分
用SQLiteOpenHelper试试,  
引用 3 楼 li352558693 的回复:

用SQLiteOpenHelper试试,  

自学中,其实不知道怎么改好。

引用 4 楼 niaogealice_123 的回复:
Quote: 引用 3 楼 li352558693 的回复:

用SQLiteOpenHelper试试,  

自学中,其实不知道怎么改好。

你百度下SQLiteOpenHelper的使用方法看看,

截下错误信息上来,估计sql有问题

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么Cursor cursor = database.rawQuery一直报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!