我已经在堆栈溢出中搜索了类似的问题,但是当我遇到问题时,它们似乎都没有应用或工作。我有一个创建SQLite数据库的应用程序,当它涉及到表格#34; LOGIN"时,它完全正常运行,但其他一切只会崩溃应用程序。我的数据库由用户以及用户可以创建的项目和任务组成,因此当我尝试列出特定用户完成的每个项目时,我得到一个"没有这样的表"即使表绝对存在并且它与LOGIN一起使用也会出错。我首先想到我还有一个旧的数据库仍在缓存中,但我已经尝试重置设备两次,这会清除整个数据库,但错误仍然存在。怎么了?
数据库助手:
package com.example.tasksketchgui;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
这是我用来访问数据库的适配器。
package com.example.tasksketchgui;
import java.util.ArrayList;
import java.util.List;
import com.example.tasksketchgui.sqlite.model.ProjectEntry;
import com.example.tasksketchgui.sqlite.model.UserEntry;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
//Column for all tables
static final String KEY_ID = "_id";
static final String KEY_NAME = "name";
static final String KEY_DEADLINE = "deadline";
static final String KEY_USER_ID = "user_id";
static final String KEY_PROJECT_ID = "project_id";
//Column names for login
static final String KEY_USER_PASSWORD = "PASSWORD";
static final String KEY_USER_NAME = "USERNAME";
//Column names for user_task
static final String KEY_TASK_ID = "task_id";
//Table names
static final String TABLE_PROJECT = "projects";
static final String TABLE_TASK = "tasks";
static final String TABLE_TASK_USER = "task_user";
static final String TABLE_PROJECT_USER = "project_user";
//Create table for projects
static final String CREATE_TABLE_PROJECT = "CREATE TABLE IF NOT EXISTS " + TABLE_PROJECT + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT, "
+ KEY_DEADLINE + " TEXT, " + KEY_USER_ID + " INTEGER" + ") ;";
//Create table for tasks
static final String CREATE_TABLE_TASK = "CREATE TABLE IF NOT EXISTS " + TABLE_TASK + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT, "
+ KEY_PROJECT_ID + " INTEGER, " + KEY_DEADLINE + " DATETIME" + ") ;";
//Create table for taskUsers
static final String CREATE_TABLE_TASK_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_TASK_USER + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_TASK_ID +
" INTEGER, " + KEY_USER_ID + "INTEGER" + ") ;";
//Create table for projectUsers
static final String CREATE_TABLE_PROJECT_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_PROJECT_USER + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PROJECT_ID + " INTEGER, "
+ KEY_USER_ID + " INTEGER" + ") ;";
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table IF NOT EXISTS "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); "
+ CREATE_TABLE_PROJECT + "; " + CREATE_TABLE_TASK + ";" + CREATE_TABLE_TASK_USER
+ ";" + CREATE_TABLE_PROJECT_USER + ";";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(UserEntry user)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", user.getUserName());
newValues.put("PASSWORD", user.getPassword());
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public void insertEntry(String userName,String password)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteUser(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeUserPassword(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
//Returns all users in the database
public List<UserEntry> getAllUser(){
List<UserEntry> users = new ArrayList<UserEntry>();
String query = "SELECT * FROM LOGIN";
Cursor cursor = db.rawQuery(query, null);
//If there's at least one entry
if(cursor.moveToFirst()){
do{
UserEntry user = new UserEntry();
user.setUserName(cursor.getString(cursor.getColumnIndex("USERNAME")));
user.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));
//Add to list
users.add(user);
}while(cursor.moveToNext());
cursor.close();
}
return users;
}
//Returns all projects from the user
public List<ProjectEntry> getProjectsFromUser(String userName){
List<ProjectEntry> projects = new ArrayList<ProjectEntry>();
String selectQuery = "SELECT * FROM " + TABLE_PROJECT + " pr," +
"LOGIN lg, " + TABLE_PROJECT_USER + " pu WHERE LOGIN.USERNAME = '"
+ userName + "' AND pr." + KEY_ID + " = pu." + KEY_PROJECT_ID +
" AND pu." + KEY_USER_ID + " = LOGIN.ID";
Cursor c = db.rawQuery(selectQuery, null);
if(c.moveToFirst()){
do{
ProjectEntry project = new ProjectEntry();
project.setId(c.getInt(c.getColumnIndex(KEY_ID)));
project.setName(c.getString(c.getColumnIndex(KEY_NAME)));
project.setUserId(c.getInt(c.getColumnIndex(KEY_USER_ID)));
project.setDeadline(c.getString(c.getColumnIndexOrThrow(KEY_DEADLINE)));
projects.add(project);
}while(c.moveToNext());
}
return projects;
}
//Create project
public long createProject(ProjectEntry project, UserEntry user){
ContentValues valuesProject = new ContentValues();
ContentValues valuesProjectUser = new ContentValues();
valuesProject.put(KEY_USER_ID, user.getId());
valuesProject.put(KEY_NAME, project.getName());
valuesProject.put(KEY_DEADLINE, project.getDeadline().toString());
long id = db.insert(TABLE_PROJECT, null, valuesProject);
valuesProjectUser.put(KEY_PROJECT_ID, id);
valuesProjectUser.put(KEY_USER_ID, user.getId());
//Return the projects id, so that it can be found
return id;
}
public long createProject(ProjectEntry project, long user){
ContentValues values = new ContentValues();
values.put(KEY_USER_ID, user);
values.put(KEY_NAME, project.getName());
values.put(KEY_DEADLINE, project.getDeadline().toString());
long id = db.insert(TABLE_PROJECT, null, values);
//Return the projects id, so that it can be found
return id;
}
public void updateEntry(String userName,String password)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
这是调用查询的代码行。
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
//Fill list of projects
List<ProjectEntry> projects = new ArrayList<ProjectEntry>();
projects = loginDataBaseAdapter.getProjectsFromUser("pontan");
这是堆栈跟踪:
08-08 15:28:07.101:D / libEGL(2213):已加载/vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 08-08 15:28:07.101:D / libEGL(2213):已加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 08-08 15:28:07.109:D / libEGL(2213):已加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 08-08 15:28:07.187:D / OpenGLRenderer(2213):启用调试模式0 08-08 15:28:07.312:D / dalvikvm(2213):GC_CONCURRENT释放114K,2%免费9097K / 9240K,暂停6ms + 17ms,总计53ms 08-08 15:28:38.179:D / dalvikvm(2213):GC_CONCURRENT释放152K,2%免费9341K / 9520K,暂停2ms + 5ms,总计23ms 08-08 15:28:39.695:E / SQLiteLog(2213):( 1)没有这样的表:项目 08-08 15:28:39.695:D / AndroidRuntime(2213):关闭VM 08-08 15:28:39.695:W / dalvikvm(2213):threadid = 1:线程退出未捕获异常(组= 0x41336930) 08-08 15:28:39.710:E / AndroidRuntime(2213):致命异常:主要 08-08 15:28:39.710:E / AndroidRuntime(2213):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.tasksketchgui / com.example.tasksketchgui.ProjectListActivity}:android.database.sqlite.SQLiteException :没有这样的表:projects(代码1):,同时编译:SELECT * FROM projects pr,LOGIN lg,project_user pu WHERE LOGIN.USERNAME =&#39; pontan&#39; AND pr._id = pu.project_id AND pu.user_id = LOGIN.ID 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.os.Handler.dispatchMessage(Handler.java:99) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.os.Looper.loop(Looper.java:137) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread.main(ActivityThread.java:5039) 08-08 15:28:39.710:E / AndroidRuntime(2213):at java.lang.reflect.Method.invokeNative(Native Method) 08-08 15:28:39.710:E / AndroidRuntime(2213):at java.lang.reflect.Method.invoke(Method.java:511) 08-08 15:28:39.710:E / AndroidRuntime(2213):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 08-08 15:28:39.710:E / AndroidRuntime(2213):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-08 15:28:39.710:E / AndroidRuntime(2213):at dalvik.system.NativeStart.main(Native Method) 08-08 15:28:39.710:E / AndroidRuntime(2213):引起:android.database.sqlite.SQLiteException:没有这样的表:projects(代码1):,同时编译:SELECT * FROM projects pr,LOGIN lg, project_user pu WHERE LOGIN.USERNAME =&#39; pontan&#39; AND pr._id = pu.project_id AND pu.user_id = LOGIN.ID 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 08-08 15:28:39.710:E / AndroidRuntime(2213):at com.example.tasksketchgui.LoginDataBaseAdapter.getProjectsFromUser(LoginDataBaseAdapter.java:167) 08-08 15:28:39.710:E / AndroidRuntime(2213):at com.example.tasksketchgui.ProjectListActivity.onCreate(ProjectListActivity.java:34) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.Activity.performCreate(Activity.java:5104) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-08 15:28:39.710:E / AndroidRuntime(2213):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 08-08 15:28:39.710:E / AndroidRuntime(2213):... 11更多 08-08 15:28:42.421:I / Process(2213):发送信号。 PID:2213 SIG:9 08-08 15:28:42.656:D / libEGL(2253):已加载/vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 08-08 15:28:42.664:D / libEGL(2253):已加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 08-08 15:28:42.664:D / libEGL(2253):已加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 08-08 15:28:42.742:D / OpenGLRenderer(2253):启用调试模式0 08-08 15:28:42.765:D / dalvikvm(2253):GC_CONCURRENT释放121K,2%免费9097K / 9248K,暂停3ms + 6ms,总计24ms
是的,其中很多是代码完全从教程中删除,还有评论,很快就会被重写。表格如何&#34; LOGIN&#34;在考虑其余部分时,确定是随机的。
我感谢所有得到的帮助!谢谢! : - )
答案 0 :(得分:2)
execSQL()
一次只能执行一条语句。
不要将SQL组合成由DATABASE_CREATE
分隔的单个;
字符串。相反,execSQL()
单独的SQL语句。
同时卸载您的应用,以便删除包含不正确表格的旧数据库。
答案 1 :(得分:0)
您是否一次性添加了表格? 如果您尚未删除该应用并重新安装它,则不会再次调用您的SQLiteOpenHelper的onCreate。
答案 2 :(得分:0)
您正在使用LOGIN lg,并使用LOGIN.USERNAME
,这也会让您 no such column exception
,而不是将其引用为
SELECT * FROM projects pr,LOGIN lg, project_user pu WHERE LOGIN.USERNAME = 'pontan' AND pr._id = pu.project_id AND pu.user_id = LOGIN.ID
使用
SELECT * FROM projects pr,LOGIN lg, project_user pu WHERE lg.USERNAME = 'pontan' AND pr._id = pu.project_id AND pu.user_id = lg.ID
您的查询应该是:
String selectQuery = "SELECT * FROM " + TABLE_PROJECT + " pr," +
"LOGIN lg, " + TABLE_PROJECT_USER + " pu WHERE lg.USERNAME = '"
+ userName + "' AND pr." + KEY_ID + " = pu." + KEY_PROJECT_ID +
" AND pu." + KEY_USER_ID + " = lg.ID";