gpt4 book ai didi

java - SQL数据库错误: Table has no column name

转载 作者:行者123 更新时间:2023-11-29 22:00:52 26 4
gpt4 key购买 nike

我浏览了很多论坛答案,通常问题不是在应该的位置添加“,”或空格,这会将 SQL 语句更改为不应该的内容。

我尝试查看我的,但仍然找不到 onCreate 中的错误。而且我每次都是用Genymotion来模拟应用程序所以不可能是模拟器有以前的数据库版本,所以onUpgrade方法应该不会有问题。

这是日志猫

09-19 07:54:26.137    7344-7344/com.example.cartermah.mywishlist E/SQLiteLog﹕ (1) table wishes has no column named content
09-19 07:54:26.137 7344-7344/com.example.cartermah.mywishlist E/SQLiteDatabase﹕ Error inserting content=jalksjldk title=HIHI recorddate=1442663666139
android.database.sqlite.SQLiteException: table wishes has no column named content (code 1): , while compiling: INSERT INTO wishes(content,title,recorddate) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at data.DatabaseHandler.addWishes(DatabaseHandler.java:67)
at com.example.cartermah.mywishlist.MainActivity.saveToDB(MainActivity.java:53)
at com.example.cartermah.mywishlist.MainActivity.access$000(MainActivity.java:15)
at com.example.cartermah.mywishlist.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
09-19 07:54:26.137 7344-7344/com.example.cartermah.mywishlist V/Wished saved!﹕ yeah

数据库处理程序代码是

package data;

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;
import android.widget.Toast;

import com.example.cartermah.mywishlist.MainActivity;

import java.sql.Date;
import java.util.ArrayList;

import model.MyWish;

/**
* Created by CarterMah on 14/09/2015.
*/
public class DatabaseHandler extends SQLiteOpenHelper {


public final ArrayList<MyWish> wishList = new ArrayList<>();


public DatabaseHandler(Context context) {
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION );
}

@Override
public void onCreate(SQLiteDatabase db) {

//This is where the table is created

String CREATE_WISHES_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "(" + Constants.KEY_ID +
" INTEGER PRIMARY KEY, " + Constants.TITLE_NAME + " TEXT, " + Constants.CONTENT_NAME +
" TEXT, " + Constants.DATE_NAME + " LONG" + ");";



db.execSQL(CREATE_WISHES_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);

//Create a new table

onCreate(db);

}

public void addWishes (MyWish wish) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(Constants.TITLE_NAME, wish.getTitle());
values.put(Constants.CONTENT_NAME, wish.getContent());
values.put(Constants.DATE_NAME, java.lang.System.currentTimeMillis());

db.insert(Constants.TABLE_NAME, null, values);

Log.v("Wished saved!","yeah");


db.close();

}

// Get all wishes

public ArrayList<MyWish> getWishes() {

String selectQuery = "SELECT * FROM " + Constants.TABLE_NAME;

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID,
Constants.TITLE_NAME, Constants.CONTENT_NAME, Constants.DATE_NAME,},
null, null, null, null, Constants.DATE_NAME + " DESC");


// loop through cursor to get every row of data

if (cursor.moveToFirst()) {

do{

MyWish wish = new MyWish();
wish.setTitle(cursor.getString(cursor.getColumnIndex(Constants.TITLE_NAME)));
wish.setContent(cursor.getString(cursor.getColumnIndex(Constants.CONTENT_NAME)));

java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance();
String dataData = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Constants.DATE_NAME))).getTime());


wish.setRecordDate(dataData);

wishList.add(wish);

}while (cursor.moveToNext());
}



return wishList;

}

}

常量.java

public class Constants {

public static final String DATABASE_NAME = "wishdb";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "wishes";
public static final String TITLE_NAME = "title";
public static final String CONTENT_NAME = "content";
public static final String DATE_NAME = "recorddate";
public static final String KEY_ID = "_id";


}

主要 Activity

package com.example.cartermah.mywishlist;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import data.DatabaseHandler;
import model.MyWish;

public class MainActivity extends AppCompatActivity {

private EditText title;
private EditText content;
private Button saveButton;
private DatabaseHandler dba;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dba = new DatabaseHandler(MainActivity.this);

title = (EditText)findViewById(R.id.titleEditText);
content = (EditText)findViewById(R.id.wishEditText);
saveButton = (Button)findViewById(R.id.saveButton);

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveToDB();
}
});






}

private void saveToDB() {

MyWish wish = new MyWish();
wish.setTitle(title.getText().toString().trim());
wish.setContent(content.getText().toString().trim());

dba.addWishes(wish);
dba.close();

// clear the form once users click the save button.

title.setText("");
content.setText("");

// Intent i = new Intent(MainActivity.this, WishDetailActivity.class);
// startActivity(i);

}


}

最佳答案

插入时需要添加id,因为id不是自增的。或者使id自增。

关于java - SQL数据库错误: Table has no column name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com