gpt4 book ai didi

android - Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE

转载 作者:行者123 更新时间:2023-12-03 18:15:19 24 4
gpt4 key购买 nike

通过将新项目添加到数据库中,我得到此错误:

靠近“)”:语法错误(代码1):,正在编译:CREATE TABLE事件(名称TEXT,art TEXT,tag TEXT primarymonat TEXT primaryjahr TEXT primary)

我做错什么了?找不到错误..

public class database extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "eventsManager";

// Contacts table name
private static final String TABLE_EVENTS = "Events";

// Contacts Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_ART = "art";
private static final String KEY_TAG = "tag";
private static final String KEY_MONAT = "monat";
private static final String KEY_JAHR = "jahr";

public database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
+ KEY_NAME + " TEXT," + KEY_ART + " TEXT,"
+ KEY_TAG + " TEXT primary" + KEY_MONAT + " TEXT primary" + KEY_JAHR + " TEXT primary"+ ")";
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_EVENTS);

// Create tables again
onCreate(db);
}

/**
* All CRUD(Create, Read, Update, Delete) Operations
*/

// Adding new contact
void addContact(events contact) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Event Name
values.put(KEY_ART, contact.getArt());
values.put(KEY_TAG, contact.getTag());
values.put(KEY_MONAT, contact.getMonat());
values.put(KEY_JAHR, contact.getJahr());

// Inserting Row
db.insert(TABLE_EVENTS, null, values);
db.close(); // Closing database connection
} }


Logcat:

    10-24 20:11:38.360    2107-2107/com.example.michael... D/AndroidRuntime﹕ Shutting down VM
10-24 20:11:38.360 2107-2107/com.example.michael... W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40ca3930)
10-24 20:11:38.370 2107-2107/com.example.michael... E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.michael.../com.example.michael...e.FullscreenActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Events(name TEXT,art TEXT,tag TEXT primarymonat TEXT primaryjahr TEXT primary)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Events(name TEXT,art TEXT,tag TEXT primarymonat TEXT primaryjahr TEXT primary)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
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.executeSql(SQLiteDatabase.java:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at com.example.michael....database.onCreate(database.java:42)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.michael....database.addContact(database.java:61)
at com.example.michael....FullscreenActivity.onCreate(FullscreenActivity.java:149)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

最佳答案

据我所知,一个表只能有一个主键。您是否要创建复合主键?

下面给出的代码用于创建组合键

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
+ KEY_NAME + " TEXT," + KEY_ART + " TEXT,"
+ KEY_TAG + " TEXT ," + KEY_MONAT + " TEXT ," + KEY_JAHR + " TEXT ,"+
"PRIMARY KEY("+KEY_TAG+","+KEY_MONAT+","+KEY_JAHR +"))";


或者,您可以将KEY_TAG,KEY_MONAT,KEY_JAHR中的任意两个设置为UNIQUE NOT NULL,并将其余一个设置为PRIMARY KEY。 UNIQUE和PRIMARY KEY之间的区别在于UNIQUE可以为null,PRIMARY KEY不能为null。一个表可以有任意数量的具有唯一约束的列,但只能有一个主键...

关于android - Android-SQLite-语法错误(代码1):,而在编译时:CREATE TABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553760/

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