i am having errors accessing the database through an android app. i am getting
我正在通过一个android应用程序访问数据库错误。
11-30 21:31:17.452: E/SQLiteLog(17817): (1) no such table: draws
E/SQLiteLog(17817):(1)没有这样的表格:平局。
11-30 21:31:17.452: E/DataAdapter(17817): getTestData >>android.database.sqlite.SQLiteException: no such table: draws (code 1): , while compiling: SELECT drawnumb, b1, b2, b3, b4, b5, b6 FROM draws WHERE drawnumb = 1221
E/DataAdapter(17817): getTestData >>android.database.sqlite。SQLiteException:没有这样的表:draw(代码1):在编译时:选择draw, b1, b2, b3, b4, b5, b6从draw= 1221中提取。
not sure what the problem is. if someone could light up the problem for me. here are chunks of my code i believe are useful. if someone asks for more code please let me know.
不知道是什么问题。如果有人能帮我解决这个问题。以下是我认为有用的代码块。如果有人要求更多的代码,请告诉我。
any help is appreciated.
任何帮助都是感激。
here is the the 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;
}
}
here is the fragment from the databaseHelper, where i am referencing the database test2.sqlite
这里是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;
this is where i am using the database to access a draw in the database and i am sure it exists
这是我使用数据库访问数据库中的一个draw的地方,我确信它是存在的。
//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);
}
});
this should return the draw number 1221 as seen in the method found in my dataAdapter class
这将返回在我的dataAdapter类中找到的方法中所看到的绘制数字1221。
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;
}
}
1 个解决方案
#1
1
This is why your table is not created:
这就是为什么没有创建表的原因:
db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_DRAWS + ...
You forgot to insert a space after the word EXISTS
.
This is what you get without space: CREATE TABLE IF NOT EXISTSdraws
.
您忘记在单词存在之后插入空格。这就是没有空间的情况:如果没有存在,创建表。
So it works:
所以它的工作原理:
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_DRAWS + ...
#1
1
This is why your table is not created:
这就是为什么没有创建表的原因:
db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_DRAWS + ...
You forgot to insert a space after the word EXISTS
.
This is what you get without space: CREATE TABLE IF NOT EXISTSdraws
.
您忘记在单词存在之后插入空格。这就是没有空间的情况:如果没有存在,创建表。
So it works:
所以它的工作原理:
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_DRAWS + ...