热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android表创建失败(接近“自动增量”:语法错误)?-AndroidtablecreationFailure(near“autoincrement”:syntaxerror)?

publicstaticfinalStringMYDATABASE_NAMEMY_DATABASE;publicstaticfinalStringMYDATABASE_
 public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final String MYDATABASE_TABLE2 = "MY_TABLE2";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_ID2 = "_id2";
 public static final String KEY_CONTENT1 = "Content1";
 public static final String KEY_CONTENT2 = "Content2";
 public static final String KEY_CONTENT3 = "Content3";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" 
                                                        +KEY_ID + " integer primary key autoincrement, " 
                                                        + KEY_CONTENT1 + " text not null);";

 private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" 
                                                        + KEY_ID2 + " integer autoincrement, " 
                                                        + KEY_CONTENT2 + " text not null, " 
                                                        + KEY_CONTENT3 + " text not null, "
                                                        + " FOREIGN KEY ("+KEY_ID2+") REFERENCES "+MYDATABASE_TABLE+" ("+KEY_ID+"));";

I can not find out what gives the following error, please help me out thank you.

我不知道是什么原因导致了以下的错误,请帮助我一下谢谢。

09-29 13:41:19.760: ERROR/Database(334): Failure 1 (near "autoincrement": syntax error) on 0x218df0 when preparing 'create table MY_TABLE2 (_id2 integer autoincrement, Content2 text not null, Content3 text not null, FOREIGN KEY (_id2) REFERENCES MY_TABLE (_id));'.

错误/数据库(334):在准备“创建表MY_TABLE2 (_id2整数自动递增,Content2文本不为空,Content3文本不为空,外键(_id2)引用MY_TABLE (_id))”时,0x218df0上的失败1(接近“自动递增”:语法错误);

09-29 13:41:19.770: DEBUG/AndroidRuntime(334): Shutting down VM

调试/ android系统(334):关闭VM

09-29 13:41:19.770: WARN/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

警告/dalvikvm(334): threadid=1:带未捕获异常的线程退出(组=0x4001d800)

09-29 13:41:19.791: ERROR/AndroidRuntime(334): FATAL EXCEPTION: main

错误/安卓时间(334):致命异常:主要

09-29 13:41:19.791: ERROR/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.com/sep.com.SepActivity}: android.database.sqlite.SQLiteException: near "autoincrement": syntax error: create table MY_TABLE2 (_id2 integer autoincrement, Content2 text not null, Content3 text not null, FOREIGN KEY (_id2) REFERENCES MY_TABLE (_id));

09-29 13:41:19.791:错误/ AndroidRuntime(334):. lang。RuntimeException:无法启动活动ComponentInfo {sep.com/sep.com.SepActivity }:android.database.sqlite。SQLiteException:临近“自动递增”:语法错误:创建表MY_TABLE2 (_id2整数自动递增,Content2文本不为空,Content3文本不为空,外键(_id2)引用MY_TABLE (_id);

5 个解决方案

#1


34  

In short: In SQLite a column declared INTEGER PRIMARY KEY will autoincrement. There is no autoincrement keyword in SQLite, that is why you are getting an error.

简而言之:在SQLite中,声明为整数的列主键将自动递增。在SQLite中没有自动递增的关键字,这就是为什么会出现错误的原因。

You can find out more on SQLite FAQ.

你可以在SQLite FAQ中找到更多信息。

EDIT: just writing integer primary key it is enough. SQLite will automatically increment your ids.

编辑:只写整型主键就够了。SQLite将自动增加您的id。

EDIT2: Your onUpgrade() method should look like this :

EDIT2:你的onUpgrade()方法应该是这样的:

  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            Log.w("MyAppTag","Updating database from version " + oldVersion + " to "
                    + newVersion + " .Existing data will be lost.");
            db.execSQL("DROP TABLE IF EXISTS " + MY_TABLE);
            db.execSQL("DROP TABLE IF EXISTS " + MY_TABLE2);
            onCreate(db);
        }

#2


8  

This:

这样的:

+ KEY_ID2 + " integer autoincrement, " 

should be this:

应该是这样的:

+ KEY_ID2 + " integer primary key autoincrement, " 

If you follow the syntax diagrams for CREATE TABLE you'll see that autoincrement should only come after primary key.

如果您遵循CREATE TABLE的语法图,您将看到自动递增应该只出现在主键之后。

enter image description here enter image description here

If you want _id2 to be a foreign key then you don't want it to be auto increment at all though.

如果你想让_id2是外键,那么你就不希望它是自动递增的。

#3


3  

An above answer says that 'There is no autoincrement keyword in SQLite.' This is incorrect. There is a keyword called 'autoincrement' and I used it in the primary key.

上面的回答是“SQLite中没有自动递增的关键字”。这是不正确的。有一个关键词叫做“自动递增”,我在主键中使用了它。

For example:

例如:

   static final String CREATE_LOCATION_TABLE="CREATE TABLE Location"+
            "( LocationID INTEGER PRIMARY KEY AUTOINCREMENT," +
            " RestuarantID int not null," +
            " Latitude float not null," +
            " Longitude float not null)";

Also make sure when you enter data to this table use the keyword 'null' in the place of the primary key.

还要确保在向该表输入数据时使用关键字'null'代替主键。

Like this:

是这样的:

   static final String INSERT_LOCATION= "Insert into Location values" +
            "(null,1002,6.905369,79.851514)";

#4


1  

Three things:

三件事:

  • Remove the trailing ; from the statements.
  • 消除拖尾;从语句。
  • Add "primary key" constraint to the autoincrement column
  • 向自动递增列添加“主键”约束
  • Remove autoincrement from PK columns - it will happen automatically.
  • 从PK列中删除自动增量——它将自动发生。

#5


0  

sqlite> CREATE TABLE app (a INTEGER PRIMARY KEY NOT NULL, B VARCHAR);
sqlite> insert into app (b) values ('');
sqlite> insert into app (b) values ('a');
sqlite> 
sqlite> insert into app (b) values ('b');
sqlite> insert into app (b) values ('c');
sqlite> insert into app (b) values ('d');
sqlite> select * from app;
1|
2|a
3|b
4|c
5|d
sqlite> exit;

NOTE: IN SQLite there in no AUTOINCREMENT keyword. So, you have to just use INTEGER PRIMARY KEY NOT NULL. It will automatically insert the incremented value for the attribute.

注意:在SQLite中没有自动递增关键字。你只需要使用整数主键NOT NULL。它将自动插入属性的递增值。


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • SQLite–CONSTRAINTS(约束)约束是数据列在表上执行的规则。这些是用来限制的数据类型可以进入一个表。这样可以确保数据的准确性和可靠性在数据库中。    级或表级约束可 ... [详细]
  • 有没有人用过sqlite?关于tablehasnocolumnnamedcolumn插入数据的时候报上边的错。问题是我明明有这一列。直接在sqlitedevoloper里执 ... [详细]
  • Python使用SQLite1.sqlite3的安装python2.5.x以上版本默认自带sqlite3模块。2.链接sqlite3数据库```#导入sqlite3模块import ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门
    第一节:前言(技术简介)EasyUI是一套js的前端框架利用它可以快速的开发出好看的前端系统web它是在jquery的框架基础上面现在越来越多的企业用它来开发web系统 ... [详细]
  • IhaveonedoubtinSqlite.dteTimeDataTypeisVarchar(200)inTablestructure.Iwanttogetresult ... [详细]
  • 州的先生(https:zmister.com)在很多项目中都有使用到SQLite数据库作为数据存储的工具,其中包括一些桌面图形界面程序和线上的Web应用程序。至今为止,它们都运行良 ... [详细]
author-avatar
mobiledu2502909745
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有