任何人都可以帮我找到问题。一切运行正常,直到我尝试在数据库上添加内容。这是错误日志。
10-24 03:55:21.625: E/SQLiteLog(924): (1) table contacts has no column named breed
10-24 03:55:21.645: E/SQLiteDatabase(924): Error inserting breed=null weight=null notes=null dateoffertilization=null earring=null milkproduction=null age=null
10-24 03:55:21.645: E/SQLiteDatabase(924): android.database.sqlite.SQLiteException: table contacts has no column named breed (code 1): , while compiling: INSERT INTO contacts(breed,weight,notes,dateoffertilization,earring,milkproduction,age) VALUES (?,?,?,?,?,?,?)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-24 03:55:21.645: E/SQLiteDatabase(924): at farm.admin.DatabaseHandler.Add_Contact(DatabaseHandler.java:75)
10-24 03:55:21.645: E/SQLiteDatabase(924): at farm.admin.Add_Update_User$1.onClick(Add_Update_User.java:146)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.view.View.performClick(View.java:4240)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.view.View$PerformClick.run(View.java:17721)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.os.Handler.handleCallback(Handler.java:730)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.os.Looper.loop(Looper.java:137)
10-24 03:55:21.645: E/SQLiteDatabase(924): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 03:55:21.645: E/SQLiteDatabase(924): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 03:55:21.645: E/SQLiteDatabase(924): at java.lang.reflect.Method.invoke(Method.java:525)
10-24 03:55:21.645: E/SQLiteDatabase(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 03:55:21.645: E/SQLiteDatabase(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 03:55:21.645: E/SQLiteDatabase(924): at dalvik.system.NativeStart.main(Native Method)
这就是代码。
package farm.admin;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_EARRING = "earring";
private static final String KEY_AGE = "age";
private static final String KEY_WEIGHT = "weight";
private static final String KEY_MILKPRODUCTION = "milkproduction";
private static final String KEY_BREED = "breed";
private static final String KEY_DATEOFFERTILIZATION = "dateoffertilization";
private static final String KEY_NOTES = "notes";
private final ArrayList<Contact> contact_list = new ArrayList<Contact>();
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_EARRING + " TEXT," + KEY_AGE + " TEXT," + KEY_WEIGHT + " TEXT" + KEY_MILKPRODUCTION + " TEXT," + KEY_BREED + " TEXT," + KEY_DATEOFFERTILIZATION + " TEXT" + KEY_NOTES + " TEXT," + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
public void Add_Contact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EARRING, contact.getEarring()); // Contact earring
values.put(KEY_AGE, contact.getAge()); // Contact age
values.put(KEY_WEIGHT, contact.getWeight()); // Contact weight
values.put(KEY_MILKPRODUCTION, contact.getMilkproduction()); // Contact milkprodcution
values.put(KEY_BREED, contact.getBreed()); // Contact breed
values.put(KEY_DATEOFFERTILIZATION, contact.getDateoffertilization()); // Contact date
values.put(KEY_NOTES, contact.getNotes()); // Contact notes
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact Get_Contact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_EARRING, KEY_AGE, KEY_WEIGHT,KEY_MILKPRODUCTION, KEY_BREED, KEY_DATEOFFERTILIZATION, KEY_NOTES }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3),cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
// return contact
cursor.close();
db.close();
return contact;
}
// Getting All Contacts
public ArrayList<Contact> Get_Contacts() {
try {
contact_list.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setEarring(cursor.getString(1));
contact.setAge(cursor.getString(2));
contact.setWeight(cursor.getString(3));
contact.setMilkproduction(cursor.getString(4));
contact.setBreed(cursor.getString(5));
contact.setDateoffertilization(cursor.getString(6));
contact.setNotes(cursor.getString(7));
// Adding contact to list
contact_list.add(contact);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
db.close();
return contact_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_contact", "" + e);
}
return contact_list;
}
// Updating single contact
public int Update_Contact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EARRING, contact.getEarring());
values.put(KEY_AGE, contact.getAge());
values.put(KEY_WEIGHT, contact.getWeight());
values.put(KEY_MILKPRODUCTION, contact.getMilkproduction());
values.put(KEY_BREED, contact.getBreed());
values.put(KEY_DATEOFFERTILIZATION, contact.getDateoffertilization());
values.put(KEY_NOTES, contact.getNotes());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void Delete_Contact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
// Getting contacts Count
public int Get_Total_Contacts() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
如果有人能帮助我指出这个问题,那就太棒了。我确信我在桌面创作上搞砸了。可能是逗号或空格出了问题。
EDIT1
经过一些建议我尝试添加一些丢失的逗号,删除最后一个并重新安装app.Now我试图添加一些东西后,应用程序崩溃了,我总是得到消息“不幸的是,应用程序停止工作”或类似的东西但是我得到了一个新的致命异常。这是日志。
10-24 06:32:40.521: D/dalvikvm(837): GC_FOR_ALLOC freed 81K, 5% free 2938K/3068K, paused 28ms, total 30ms
10-24 06:32:40.561: D/AndroidRuntime(837): Shutting down VM
10-24 06:32:40.571: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-24 06:32:40.581: E/AndroidRuntime(837): FATAL EXCEPTION: main
10-24 06:32:40.581: E/AndroidRuntime(837): java.lang.NullPointerException
10-24 06:32:40.581: E/AndroidRuntime(837): at farm.admin.Main_Screen$Contact_Adapter.getView(Main_Screen.java:157)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.ListView.onMeasure(ListView.java:1159)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 06:32:40.581: E/AndroidRuntime(837): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.os.Handler.handleCallback(Handler.java:730)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:137)
10-24 06:32:40.581: E/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 06:32:40.581: E/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:32:40.581: E/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:525)
10-24 06:32:40.581: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 06:32:40.581: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:32:40.581: E/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_EARRING + " TEXT," + KEY_AGE + " TEXT," + KEY_WEIGHT + " TEXT" + KEY_MILKPRODUCTION + " TEXT," + KEY_BREED + " TEXT," + KEY_DATEOFFERTILIZATION + " TEXT" + KEY_NOTES + " TEXT," + ")";
列规范之间缺少一些,
。例如。在weight
和?dateoffertilization
之后。在最后一列规范之后也不需要,
。
在此处更改架构后,要么提升DATABASE_VERSION
,要么只是擦除应用程序的数据以确保再次运行表格。