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

Unable to connect Android device using adb
Android Updating an App in Google Play
How to wire up for a long click on an image in an activity in android
Android: Developing apps that run in the 'background', how?
Listview viewholder and image
App doesn´t work on device
Android - How to animate an activity transition when the default back button is pressed
Web Page is not Available error in WebView
Android Audio Streaming from URL
How to add images from a gallery to a listview
How to get the nickname for my Android device set in Google Play Store
Cannot find proper library when creating module on samsung s3 with card io
AS3: Sound class not working on Android
Unable to lauch the android app.Logcat error
How to call custom intent page in phonegap?
String showing dot sequence after some characters in android

Categories

HOME
url-redirection
eclipse
kentico
cobalt
nvd3.js
dns
github-for-windows
amp-html
formal-languages
pda
pyqt
oracle-apex-5
grafana
carousel
ado.net
jetty
mapbox
accessibility
openstreetmap
appcelerator-studio
pega
xcodebuild
rtf
naivebayes
docx
microsoft-dynamics-nav
symfony-console
mapstruct
connector
asp.net-core-webapi
angular-routing
imageresizer
php-mysqlidb
highstock
gpio
pylons
licensing
asymptotic-complexity
android-things
errbot
database-comparison
jbutton
r-grid
laravel-eloquent
jett
sammy.js
login-script
sharding
modalviewcontroller
jwplayer6
python-pptx
flatmap
gdata
linq-to-objects
contract
rfc3339
optimizely
puredata
httrack
textsum
tinymce-3
aquamacs
flyout
restore
preprocessor
jodd
dup
xdoclet
asyncdisplaykit
libvlc
vhosts
xenomai
page.js
haskell-warp
linked-tables
dnssec
alloy-ui
procedural-programming
dbaccess
filesplitting
sql-scripts
rickshaw
tweenlite
with-statement
angular-carousel
mintty
paste
vbe
koala
accesscontrolexception
notorm
mvc-editor-templates
merb
video-codecs
superscrollorama
chart-director
patricia-trie
fragmenttransaction
pyunit
landscape-portrait
tournament
vs-android
zen
filesystemobject
perl5.12
todos

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