1. Database Problem
- Posted by Lone_EverGreen_Ranger Feb 07, 2011
- 1412 views
Hello All,
I am having trouble getting my database to work. I'll post my code and see if any of you can help. When I run it, it says cannot open database. I am using Eu 4.0
include std/eds.e include std/get.e include std/sort.e include std/filesys.e include std/io.e include std/os.e include Include/herodata.e include Include/enemydata.e include Include/bossdata.e include Include/vehicledata.e include Include/itemdata.e include Include/element.e include Include/attributedata.e include Include/npcdata.e include Include/mapdata.e include Include/weapondata.e include Include/shopdata.e include Include/attackdata.e include Include/spelldata.e include Include/skilldata.e include Include/sedata.e include Include/bgdata.e include Include/scdata.e include Include/tiledata.e include Include/classdata.e include Include/gamedata.e if db_create("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Database could not be created") abort(1) end if if db_open("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not open database") abort(1) end if if db_select("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not select database") abort(1) end if if db_create_table("RPGDataTable") != DB_OK then puts(1,"Could not create table") abort(1) end if if db_select_table("RPGDataTable") != DB_OK then puts(1,"Could not select table") abort(1) end if if db_insert(MAP_NAME, Map_Name) != DB_OK then puts(1,"Failed to insert data") abort(1) end if
2. Re: Database Problem
- Posted by bruce1961 Feb 07, 2011
- 1390 views
it says cannot open database.
... if db_create("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Database could not be created") abort(1) end if if db_open("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not open database") abort(1) end if
Does the file get populated and closed between the create and open?
Bruce.
3. Re: Database Problem
- Posted by DerekParnell (admin) Feb 07, 2011
- 1424 views
When I run it, it says cannot open database. I am using Eu 4.0
The reason is that when a database is created it is also opened, so opening a database that is already opened by your application will fail.
Try this instead ...
include std/eds.e include std/get.e include std/sort.e include std/filesys.e include std/io.e include std/os.e include Include/herodata.e include Include/enemydata.e include Include/bossdata.e include Include/vehicledata.e include Include/itemdata.e include Include/element.e include Include/attributedata.e include Include/npcdata.e include Include/mapdata.e include Include/weapondata.e include Include/shopdata.e include Include/attackdata.e include Include/spelldata.e include Include/skilldata.e include Include/sedata.e include Include/bgdata.e include Include/scdata.e include Include/tiledata.e include Include/classdata.e include Include/gamedata.e if db_create("RPGData", DB_LOCK_NO) != DB_OK then if db_open("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not open database") abort(1) end if end if if db_select("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not select database") abort(1) end if if db_create_table("RPGDataTable") != DB_OK then puts(1,"Could not create table") abort(1) end if if db_select_table("RPGDataTable") != DB_OK then puts(1,"Could not select table") abort(1) end if if db_insert(MAP_NAME, Map_Name) != DB_OK then puts(1,"Failed to insert data") abort(1) end if
4. Re: Database Problem
- Posted by Lone_EverGreen_Ranger Feb 07, 2011
- 1502 views
When I run it, it says cannot open database. I am using Eu 4.0
The reason is that when a database is created it is also opened, so opening a database that is already opened by your application will fail.
Try this instead ...
include std/eds.e include std/get.e include std/sort.e include std/filesys.e include std/io.e include std/os.e include Include/herodata.e include Include/enemydata.e include Include/bossdata.e include Include/vehicledata.e include Include/itemdata.e include Include/element.e include Include/attributedata.e include Include/npcdata.e include Include/mapdata.e include Include/weapondata.e include Include/shopdata.e include Include/attackdata.e include Include/spelldata.e include Include/skilldata.e include Include/sedata.e include Include/bgdata.e include Include/scdata.e include Include/tiledata.e include Include/classdata.e include Include/gamedata.e if db_create("RPGData", DB_LOCK_NO) != DB_OK then if db_open("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not open database") abort(1) end if end if if db_select("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not select database") abort(1) end if if db_create_table("RPGDataTable") != DB_OK then puts(1,"Could not create table") abort(1) end if if db_select_table("RPGDataTable") != DB_OK then puts(1,"Could not select table") abort(1) end if if db_insert(MAP_NAME, Map_Name) != DB_OK then puts(1,"Failed to insert data") abort(1) end if
Thanks that worked. However, now it says it cannot create the table.
5. Re: Database Problem
- Posted by petelomax Feb 07, 2011
- 1463 views
if db_create("RPGData", DB_LOCK_NO) != DB_OK then if db_open("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not open database") abort(1) end if end if if db_select("RPGData", DB_LOCK_NO) != DB_OK then puts(1,"Could not select database") abort(1) end if if db_create_table("RPGDataTable") != DB_OK then puts(1,"Could not create table") abort(1) end if if db_select_table("RPGDataTable") != DB_OK then puts(1,"Could not select table") abort(1) end if if db_insert(MAP_NAME, Map_Name) != DB_OK then puts(1,"Failed to insert data") abort(1) end if
I've always done this
if open database fails then if create database fails then fatal/abort/message/return end if end if -- There is technically no need to select the database here, though it is wise -- and costs little to do so at the start of any code block accessing the db. if select table fails then if create table fails then fatal/abort/message/return end if end if -- read/write/close/whatever
HTH,
Pete
6. Re: Database Problem
- Posted by DerekParnell (admin) Feb 07, 2011
- 1442 views
I've always done this
if open database fails then if create database fails then fatal/abort/message/return end if end if -- There is technically no need to select the database here, though it is wise -- and costs little to do so at the start of any code block accessing the db. if select table fails then if create table fails then fatal/abort/message/return end if end if -- read/write/close/whatever
Yes, that is a better method.
The 'create_table' was probably failing because the table already existed from an earlier run of the program.
7. Re: Database Problem
- Posted by Lone_EverGreen_Ranger Feb 07, 2011
- 1326 views
I've always done this
if open database fails then if create database fails then fatal/abort/message/return end if end if -- There is technically no need to select the database here, though it is wise -- and costs little to do so at the start of any code block accessing the db. if select table fails then if create table fails then fatal/abort/message/return end if end if -- read/write/close/whatever
Yes, that is a better method.
The 'create_table' was probably failing because the table already existed from an earlier run of the program.
Thanks for the help, I got it.