1. Database Problem

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 
new topic     » topic index » view message » categorize

2. Re: Database Problem

Lone_EverGreen_Ranger said...

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Database Problem

Lone_EverGreen_Ranger said...

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 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Database Problem

DerekParnell said...
Lone_EverGreen_Ranger said...

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.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Database Problem

Lone_EverGreen_Ranger said...
DerekParnell said...
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

new topic     » goto parent     » topic index » view message » categorize

6. Re: Database Problem

petelomax said...

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Database Problem

DerekParnell said...
petelomax said...

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu