当我尝试插入数据库时,我不断收到以下logcat错误:
07-22 23:52:28.039: E/SQLiteLog(12345): (1) table feelings_needs_table has no column named needs
07-22 23:52:28.190: E/SQLiteDatabase(12345): Error inserting needs={need=peace} feelings={feeling=tired}
07-22 23:52:28.190: E/SQLiteDatabase(12345): android.database.sqlite.SQLiteException: table feelings_needs_table has no column named needs (code 1): , while compiling: INSERT INTO feelings_needs_table(needs,feelings) VALUES (?,?)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at com.example.feeling.DatabaseConnectToActivities.insertEmotions(DatabaseConnectToActivities.java:42)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at com.example.feeling.Needs$1.onItemClick(Needs.java:79)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.widget.AbsListView$1.run(AbsListView.java:3423)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.os.Handler.handleCallback(Handler.java:725)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.os.Looper.loop(Looper.java:137)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 23:52:28.190: E/SQLiteDatabase(12345): at dalvik.system.NativeStart.main(Native Method)
07-22 23:52:28.333: I/Choreographer(12345): Skipped 464 frames! The application may be doing too much work on its main thread.
这是我的数据库助手:
public class DatabaseHelp extends SQLiteOpenHelper {
//set up database variables
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "feelings_needs";
public static final String DATABASE_TABLE = "feelings_needs_table";
public static final String ROW_ID = "_id";
public static final String FEELING_ID = "feelings";
public static final String NEED_ID = "needs";
//creates the helper
public DatabaseHelp(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
//set-up the table
@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
String CREATE_TABLES = "CREATE TABLE DATABASE_TABLE(ROW_ID integer primary key autoincrement, "
+ "FEELING_ID text,"
+ "NEED_ID text);";
database.execSQL(CREATE_TABLES);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
database.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(database);
}
}
这是我有插入方法的地方:
public class DatabaseConnectToActivities {
private SQLiteDatabase database;
private DatabaseHelp databaseHelper; //instance of databasehelp
//public constructor for database connection
public DatabaseConnectToActivities(Context context){
//create a new database open helper
databaseHelper =
new DatabaseHelp(context);
}
public void open()throws SQLException{
database = databaseHelper.getWritableDatabase();
}
public void close(){
databaseHelper.close();
}
// inserts feeling and needs into database
public void insertEmotions(String feeling, String need){
ContentValues emotion = new ContentValues();
emotion.put(DatabaseHelp.FEELING_ID, feeling);
emotion.put(DatabaseHelp.NEED_ID, need);
open(); //open the database
database.insert(DatabaseHelp.DATABASE_TABLE, null, emotion);
close();
}
public String getData(){
//create columns and reference the rows with a cursor object
String[] columns = new String[] {DatabaseHelp.ROW_ID, DatabaseHelp.FEELING_ID ,DatabaseHelp.NEED_ID};
Cursor c = database.query(DatabaseHelp.DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(DatabaseHelp.ROW_ID);
int iFeeling = c.getColumnIndex(DatabaseHelp.FEELING_ID);
int iNeed = c.getColumnIndex(DatabaseHelp.NEED_ID);
for (c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iFeeling)+ " " + c.getString(iNeed)+ "\n";};
c.close();
return result;
}
}
这是我在我的活动中调用insert方法的地方:
public class Needs extends Activity {
String gotTheFeeling;
private DatabaseConnectToActivities database;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive string from feelingsmain
Bundle gotFeeling = getIntent().getExtras();
gotTheFeeling = gotFeeling.getString("aFeeling");
// new database object
database = new DatabaseConnectToActivities(this);
database.open();
initList();
ListView lv = (ListView) findViewById(R.id.pageLayout);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, needsList,
android.R.layout.simple_list_item_1, new String[] { "need" },
new int[] { android.R.id.text1 });
lv.setAdapter(simpleAdpt);
lv.setOnItemClickListener(backToFeelings);
}
List<Map<String, String>> needsList = new ArrayList<Map<String, String>>();
protected String feelingForDb;
public void initList() {
needsList.add(createNeed("need", "space"));
needsList.add(createNeed("need", "peace"));
needsList.add(createNeed("need", "calm"));
needsList.add(createNeed("need", "understanding"));
needsList.add(createNeed("need", "to be heard"));
needsList.add(createNeed("need", "to be seen"));
needsList.add(createNeed("need", "love"));
}
private HashMap<String, String> createNeed(String key, String name) {
HashMap<String, String> need = new HashMap<String, String>();
need.put(key, name);
return need;
}
OnItemClickListener backToFeelings = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String need = needsList.get(arg2).toString();
database.insertEmotions(gotTheFeeling, need);
database.close();
Intent toFeelings = new Intent(Needs.this, FeelingsMain.class);
startActivity(toFeelings);
}
};
感谢您提供的任何帮助。 :)
答案 0 :(得分:1)
text,
+ "FEELING_ID text,"
后需要一个空格。此外,您的完整字符串CREATE_TABLES
是错误的。您的字符串变量应该在引号之外。
正确的CREATE_TABLES
字符串是
String CREATE_TABLES = "CREATE TABLE " + DATABASE_TABLE
+ "("
+ ROW_ID + " integer primary key autoincrement, "
+ FEELING_ID + " text, "
+ NEED_ID + " text"
+");";
答案 1 :(得分:0)
在DataBaseHelper类中,您需要更改此
+ "FEELING_ID text,"
+ "NEED_ID text);";
为此:
+ FEELING_ID +" text, "
+ NEED_ID +" text);";
您需要在FEELING_ID
之外设置NEED_ID
和"
。
答案 2 :(得分:0)
此外,在修改表的结构时,请务必增加DATABASE_VERSION变量。这将清理您的数据库并重新创建它。 否则,您可以根据需要修改表格创建的语法,而不会产生任何结果。