我正在尝试在数据库中创建多个表,但第一个表只是成功创建而另外两个给了我错误我想知道如何确保创建表
04-18 17:20:45.343: E/AndroidRuntime(956): android.database.sqlite.SQLiteException: no such table: squares: , while compiling: SELECT _squareid, squarename, squarelatitude, squarelongitude FROM squares
这是我的代码
public class Dbinfo {
public static final String KEY_RAWID = "_id";
public static final String KEY_RAWNAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
public static final String KEY_SQUAREID = "_squareid";
public static final String KEY_SQUARENAME = "squarename";
public static final String KEY_SQUARELATITUDE = "squarelatitude";
public static final String KEY_SQUARELONGITUDE = "squarelongitude";
private static final String KEY_STREETID = "streetid";
private static final String KEY_STREETNAME = "streetname";
private static final String KEY_STREETLATITUDE="streetlatitude";
private static final String KEY_STREETLONGTIUDE="streetlongtiude";
private static final String KEY_STREETCAPACITY = "streetcapacity";
private static final String KEY_STREETFOREIGN = "streetforegin";
private static final String DATABASE_NAME = "users_DB";
private static final String DATABASE_TABLE = "users";
private static final String DATABASE_TABLESQUARES = "squares";
private static final String DATABASE_TABLE_STREETS = "streets";
private static final int DATABASE_VERSION = 1;
private dbusers ourhelper;
private final Context ourcontext;
private static SQLiteDatabase ourDatabase;
private static class dbusers extends SQLiteOpenHelper {
public dbusers(Context context) {
super(context, DATABASE_TABLE, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_RAWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_RAWNAME
+ " TEXT NOT NULL, " + KEY_PASSWORD + " TEXT NOT NULL, "
+ KEY_EMAIL + " TEXT NOT NULL);");
db.execSQL("CREATE TABLE " + DATABASE_TABLESQUARES + "("
+ KEY_SQUAREID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_SQUARENAME + "TEXT NOT NULL, " + KEY_SQUARELATITUDE
+ "TEXT NOT NULL, " + KEY_SQUARELONGITUDE
+ "TEXT NOT NULL);");
final String STREET = "CREATE TABLE " + DATABASE_TABLE_STREETS
+ " (" + KEY_STREETID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_STREETNAME+
" TEXT NOT NULL"+KEY_STREETLATITUDE+"TEXT NOT NULL"+KEY_SQUARELONGITUDE
+ " TEXT NOT NULL, " + KEY_STREETCAPACITY
+ " text not null, FOREIGN KEY (" + KEY_STREETFOREIGN
+ ") REFERENCES " + DATABASE_TABLESQUARES + " ("
+ KEY_SQUAREID + "));";
db.execSQL(STREET);
任何帮助??
答案 0 :(得分:0)
在sql
创建String
(表DATABASE_TABLESQUARES
)中的列名后面加一些空格:
db.execSQL("CREATE TABLE " + DATABASE_TABLESQUARES + "("
+ KEY_SQUAREID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_SQUARENAME + " TEXT NOT NULL, " + KEY_SQUARELATITUDE
+ " TEXT NOT NULL, " + KEY_SQUARELONGITUDE
+ " TEXT NOT NULL);");
完成上述修改后,您必须卸载应用并重新运行,以便再次调用onCreate
的{{1}}方法,以根据新的数据库初始化数据库sql SQLiteOpenHelper
。
另外,作为Sam的评论,还修改了表String
的sql创建String
,并在列声明之间添加了一些逗号(就像之前对其他两个表所做的那样)或将得到当你试图访问该表时再次遇到麻烦。
答案 1 :(得分:0)
首先使用SQLite浏览器检查数据库
如果找不到表,那么您在创建table.check时遇到创建
的代码问题
表。
private static final String DATABASE_CREATE3 = "create table IF NOT EXISTS "
+ TABLE_EXAM + "( " + COLUMN_EXAM_ID
+ " integer primary key autoincrement, "
+ COLUMN_SUBCHAWISE + " text not null, "
+ COLUMN_EXAM_TYPE + " text not null, "
+ COLUMN_EXAM_DETAILS + " text not null, "
+ COLUMN_MARKS + " number not null);";