我通过Android应用程序访问数据库时出错。我正在
11-30 21:31:17.452:E / SQLiteLog(17817):( 1)没有这样的表:draw
11-30 21:31:17.452:E / DataAdapter(17817):getTestData>> android.database.sqlite.SQLiteException:没有这样的表:draw(代码1):,编译时:SELECT drawnumb,b1 ,b2,b3,b4,b5,b6 FROM绘制WHERE drawnumb = 1221
不确定问题是什么。如果有人可以为我解决问题。 这里是我认为很有用的代码块。如果有人要求提供更多代码,请告诉我。
感谢任何帮助。
这是databaseHandler
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
public class databaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "drawManager",
TABLE_DRAWS = "draws",
KEY_DRAWNUMB = "drawnumb",
KEY_B1 = "b1",
KEY_B2 = "b2",
KEY_B3 = "b3",
KEY_B4 = "b4",
KEY_B5 = "b5",
KEY_B6 = "b6",
KEY_B7 = "b7",
KEY_WINNERS = "winners";
public databaseHandler (Context context){
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_DRAWS + "(" + KEY_DRAWNUMB + " INTEGER PRIMARY KEY,"+ KEY_B1 + " INTEGER," + KEY_B2 + " INTEGER,"+
KEY_B3 + " INTEGER," + KEY_B4 + " INTEGER,"+KEY_B5 + " INTEGER," + KEY_B6 + " INTEGER,"+KEY_B7 + " INTEGER," + KEY_WINNERS + " INTEGER)");
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Draw getDraw(int drawnum){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_DRAWS, new String[] {KEY_DRAWNUMB, KEY_B1, KEY_B2, KEY_B3, KEY_B4,
KEY_B5, KEY_B6, KEY_B7, KEY_WINNERS}, KEY_DRAWNUMB + "=?", new String []{String.valueOf(drawnum)} , null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Draw draw = new Draw(Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)),Integer.parseInt(cursor.getString(2)),
Integer.parseInt(cursor.getString(3)),Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),
Integer.parseInt(cursor.getString(6)),Integer.parseInt(cursor.getString(7)),Integer.parseInt(cursor.getString(8)));
db.close();
cursor.close();
return draw;
}
}
这是来自databaseHelper的片段,我在这里引用数据库test2.sqlite
public class DatabaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="test2.sqlite";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
这是我使用数据库访问数据库中的绘图的地方,我确信它存在
//database test
buttonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DataAdapter mDbHelper = new DataAdapter(getApplicationContext());
mDbHelper.createDatabase();
mDbHelper.open();
String results="";
Cursor testdata = mDbHelper.getTestData();
try {
//for (int i=1;1<7;i++){
results = testdata.getString(1);//testdata.getColumnIndex("b"+ Integer.toString(i)));
results += " "+ testdata.getString(2);
results += " "+ testdata.getString(3);
results += " "+ testdata.getString(4);
results += " "+ testdata.getString(5);
results += " "+ testdata.getString(6);
}
//results = testdata.getString(testdata.getColumnIndex("drawnumb"));
//}
catch (Exception ex) {
results = "not working";
}
//String name = Utility.GetColumnValue(testdata, "Name");
//String email = Utility.GetColumnValue(testdata, "Email");
//Utility.ShowMessageBox(this, "Name: "+ name + " and Email: "+ email);
mDbHelper.close();
//res = getRandomNumbers(numbers);
lottery.setText("Your numbers: " + results);
}
});
这应该返回绘图号1221,如我在dataAdapter类
中找到的方法中所示public Cursor getTestData()
{
try
{
String sql ="SELECT drawnumb, b1, b2, b3, b4, b5, b6 FROM draws WHERE drawnumb = 1221";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
答案 0 :(得分:1)
这就是您的表格未创建的原因:
db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_DRAWS + ...
您忘记在单词EXISTS
后插入空格。
这是你没有空间的地方:CREATE TABLE IF NOT EXISTSdraws
。
所以它有效:
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_DRAWS + ...