android


Android SQLiteHelper can't create FOREIGN KEY


I'm having some trouble with SQLite on Android. I try to create my tables that way:
Trade Shows:
String CREATE_TRADE_SHOW_TABLE = "CREATE TABLE tradeShows ( " +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"name TEXT NOT NULL, " +
"location TEXT NOT NULL, " +
"doDate DATE NOT NULL, " +
"createdDate DATETIME NOT NULL, " +
"lastModDate DATETIME NOT NULL, " +
"pictureUrl TEXT, " +
"picture BLOB )";
db.execSQL(CREATE_TRADE_SHOW_TABLE);
It's working fine, and then, Vendors:
String CREATE_TRADE_SHOW_TABLE = "CREATE TABLE vendors ( " +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"tradeShowId INTEGER NOT NULL, " +
"name TEXT NOT NULL, " +
"createdDate DATETIME NOT NULL, " +
"lastModDate DATETIME NOT NULL, " +
"pictureUrl TEXT, " +
"picture BLOB," +
"FOREIGN KEY(tradeShowId) REFERENCES tradeShows(id)" +
")";
db.execSQL(CREATE_TRADE_SHOW_TABLE);
So now my Table Vendors is created, but the tradeShowId FOREIGN KEY is not there. I dont get any error, my table is created just fine, but without the tradeShowId.
What can I be possibly doing wrong?
SQLite supports foreign keys, but not by default.
If you write for API level 16 or higher, you should do it in onConfigure method of the SQLiteOpenHelper class:
#Override
public void onConfigure(SQLiteDatabase db) {
db.setForeignKeyConstraintsEnabled(true);
}
In the previous API levels it should be done this way:
#Override
public void onOpen(SQLiteDatabase db) {
db.execSQL("PRAGMA foreign_keys = ON;");
}
You can read about this in SQLite documentation:
"Foreign key constraints are disabled by default (for backwards
compatibility), so [they] must be enabled separately for each database
connection. (Note, however, that future releases of SQLite might
change so that foreign key constraints [are] enabled by default.
Careful developers will not make any assumptions about whether or not
foreign keys are enabled by default but will instead enable or disable
them as necessary.)"

Related Links

how to set parameters to volley request (GET request )?
How to perform Push Notification in adobe Phonegap using Firebase Cloud Messaging
Mark existing string.xml values for re-translation in Android Studio?
Android app cannot be updated in Google play store
Clickable and rotatable models using Android Studio and ARToolkit by using ARNativeOSG sample project
How many parallel execution possible using executeOnExecuter?
Firebase instead of SQLite
Implementing selection page that each selection leads to different options in android
AndroidMy 6.0 targetSdkVersion 23 open failed: EACCES (Permission denied) [duplicate]
Custom LinearLayoutManager android
How to call a method after text complete in edit text in Android
Android Studio “Waiting for debugger” hangs, only on certain project
Couldn't read row 0, col 12 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it
What threading method can I use for reading long text files in Android?
how to make notification in receiver
Why is buffer return null in this example?

Categories

HOME
jpa
hyperlink
alarm
opendj
yandex
dojo
x264
banner
opengl-3
commonjs
speech-synthesis
pybossa
cumulocity
gruntfile
azure-powershell
standards
bourbon
boost-icl
oledb
breadth-first-search
ag
froala
fragment-backstack
android-maps
gp
carrierwave
jett
hivemq
alphabetical
git-rewrite-history
z-notation
projects-and-solutions
urlsession
tasm
plaintext
apriori
preloader
photos
oim
serde
robomongo
spring-integration-sftp
lightning
payara-micro
colorbar
jqchart
d3-force-directed
reachability
synonym
expectations
appscale
android-wake-lock
query-by-example
sklearn-pandas
httpruntime.cache
toran-proxy
dynatree
roadkill-wiki
xvim
coda
lasso
graphics2d
telepat
rapidsvn
primitive-types
contrast
teaspoon
extjs2
kendo-dataviz
isqlquery
font-awesome-4.0.0
dopostback
dajaxice
workflow-services
exponentiation
rmdir
zen
software-lifecycle
wcf-proxy
leader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App