在我重新安装手机上的应用程序之前,一切都很好。如有必要,我可以发布我的代码。我不知道是什么原因引起了这个问题。 这是logcat:
04-19 19:31:48.799: E/AndroidRuntime(24963): FATAL EXCEPTION: main
04-19 19:31:48.799: E/AndroidRuntime(24963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.classorganizer/com.example.classorganizer.Monday}: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.os.Looper.loop(Looper.java:130)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-19 19:31:48.799: E/AndroidRuntime(24963): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963): at java.lang.reflect.Method.invoke(Method.java:507)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-19 19:31:48.799: E/AndroidRuntime(24963): at dalvik.system.NativeStart.main(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963): Caused by: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1501)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1380)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1416)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.cookbook.data.MyDB.getdiaries(MyDB.java:66)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.example.classorganizer.Monday$DiaryAdapter.getdata(Monday.java:86)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.example.classorganizer.Monday$DiaryAdapter.<init>(Monday.java:80)
04-19 19:31:48.799: E/AndroidRuntime(24963): at com.example.classorganizer.Monday.onCreate(Monday.java:68)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 19:31:48.799: E/AndroidRuntime(24963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-19 19:31:48.799: E/AndroidRuntime(24963): ... 11 more
这是我的MyDBhelper文件:
package com.cookbook.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDBhelper extends SQLiteOpenHelper{
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
private SQLiteDatabase db;
// database initialization
public MyDBhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("MyDBhelper onCreate","Creating all the tables");
try {
this.db = db;
db.execSQL(CREATE_TABLE);
for(int i=1; i <= 48; i++) { insertdiary(db, "free",""); }
}
catch(SQLiteException ex) {
Log.v("Create table exception", ex.getMessage());
}
}
// Saves a diary entry to the database as name-value pairs in ContentValues instance
// then passes the data to the SQLitedatabase instance to do an insert
public long insertdiary(SQLiteDatabase db, String title, String content)
{
try{
ContentValues newTaskValue = new ContentValues();
newTaskValue.put(Constants.TITLE_NAME, title);
newTaskValue.put(Constants.CONTENT_NAME, content);
return db.insert(Constants.TABLE_NAME, null, newTaskValue);
} catch(SQLiteException ex) {
Log.v("Insert into database exception caught",
ex.getMessage());
return -1;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
+" to "+newVersion
+", which will destroy all old data");
db.execSQL("drop table if exists "+Constants.TABLE_NAME);
onCreate(db);
}
}
这是MyDB文件:
package com.cookbook.data;
import com.example.classorganizer.Diary;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class MyDB {
private static final String TABLE_NAME = null;
private static final String KEY_ID = null;
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;
// Initializes MyDBHelper instance
public MyDB(Context c){
context = c;
dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
Constants.DATABASE_VERSION);
}
// Closes the database connection
public void close()
{
db.close();
}
// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{
try {
db = dbhelper.getWritableDatabase();
} catch(SQLiteException ex) {
Log.v("Open database exception caught", ex.getMessage());
db = dbhelper.getReadableDatabase();
}
}
// updates a diary entry (existing row)
public boolean updateDiaryEntry(String title, long rowId)
{
ContentValues newValue = new ContentValues();
newValue.put(Constants.TITLE_NAME, title);
return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;
}
// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
Cursor c = db.query(Constants.TABLE_NAME, null, null,
null, null, null, null);
return c;
}
}
这是在列表中输出新创建的行的文件:
package com.example.classorganizer;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
ListView listView = new ListView(Monday.this);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
}
public String title;
public String content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
@Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
答案 0 :(得分:1)
问题在于这一行:
Constants.CONTENT_NAME+" text not null, "
删除最后一个逗号并添加右括号:
Constants.CONTENT_NAME+" text not null)"
答案 1 :(得分:0)
以下是您的问题:SQLiteException: no such table: diaries
。
在尝试从中进行SELECT之前,检查表是否已在数据库中创建。
答案 2 :(得分:0)
这是你的问题:
no such table: diaries
问题很可能是,因为您说您重新安装了该应用,但您尚未创建数据库或您正在选择的表格。
编辑:现在您发布了数据库创建脚本,语法错误:
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
您在Constants.TABLE_NAME打开后离开括号。在Constants.CONTENT_NAME之后,写下:+" text not null)"
答案 3 :(得分:0)
您的onCreate()
有SQL语法问题,但您抓住SQLiteException
,因此框架认为表创建成功。您不应该在sqlite帮助程序生命周期方法中捕获异常。如果出现问题,应该将其传播给调用者,而不是默默地忽略。
至于语法问题,
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
应该是
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null)"
;