简体   繁体   中英

Create a SQLite Database in Android with Foreign Keys?

Is this supported? I'm attempting to create a table but am getting a syntax error when compiling.

private static final String ROOM_CREATE =
        "create table room (_id integer primary key autoincrement, " +
        "facility integer not null, FORIEGN KEY (facility) REFERENCES facility(_id));";

您可以创建外键,但是必须使用以下命令在SQLite中启用强制

PRAGMA foreign_keys = ON;

Typo in the SQL:

FORIEGN -> FOREIGN

Some great answers, particularly @sfratini for spotting the typo in the FOREIGN keyword. Can I also recommend that you should troubleshoot your SQL statements by using a command line client, such as the "Precompiled Binaries For Windows" (see http://sqlite.org/download.html ). That way, you can validate the CREATE TABLE and INSERT statements before you put them into your app.

The facility in facility(_id) must refers to table name. You are defining same name to column also. Please change table name and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM