1. Trouble with Database(EDS)

So I am trying to create myself a little database using the EDS database. However I keep getting errors like database cannot be created, or out of bounds error. And type_check failure errors. I'm using Windows 7 Ultimate. I'll post the code I've written so far. Maybe I'm missing something or putting the code in the wrong order?

include std/eds.e 
include std/get.e 
--include std/misc.e 
 
--without type_check 
 
--namespace eds 
 
if db_create("GameDatabase",DB_LOCK_SHARED,5,5) = DB_OK then 
	puts(1,"Couldn't create database!\n") 
	abort(1) 
end if 
 
if db_create_table("gametable") = DB_OK then 
	puts(1,"Failed to create table!\n") 
end if 
 
if db_insert("Uncharted",{"Naughty Dog",50}) then 
	puts(1,"Failed to insert data!\n") 
end if 
new topic     » topic index » view message » categorize

2. Re: Trouble with Database(EDS)

  • You want the conditional to read != as in "not equal" before giving a fail message
  • After you run you code once you may have a database created (look at your directory). In this case you must open (not create) the database.

Hint: delete the database file and try again when testing the "creation phase"

_tom

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

3. Re: Trouble with Database(EDS)

Ok I got it to open after deleting the previously made database. How do I make it so I can test it without the couldn't create database error coming up again? I'm using the Eu4 version of EDS.

Revised code

include std/eds.e 
include std/get.e 
--include std/misc.e 
 
--without type_check 
 
--namespace eds 
 
 
if db_create("GameDatabase",DB_LOCK_SHARED,5,5) != DB_OK then 
	puts(1,"Couldn't create database!\n") 
	abort(1) 
end if 
 
if db_open("GameDatabase",DB_LOCK_SHARED) != DB_OK then 
	puts(1,"Couldn't open Database!\n") 
end if 
 
if db_create_table("gametable") != DB_OK then 
	puts(1,"Failed to create table!\n") 
end if 
 
--Game name, Developer, Install-Size 
if db_insert("Uncharted",{"Naughty Dog",50}) then 
	puts(1,"Failed to insert data!\n") 
	elsif db_insert("Uncharted Collection",{"Naughty Dog",50}) then 
		puts(1,"Failed to insert data!\n") 
end if 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Trouble with Database(EDS)

Icy_Viking said...

Ok I got it to open after deleting the previously made database. How do I make it so I can test it without the couldn't create database error coming up again? I'm using the Eu4 version of EDS.

Here is a quick shorthand function you can use to always open your database with the correct method.

include std/eds.e 
 
public function db_load( sequence path, integer lock_mode = DB_LOCK_EXCLUSIVE ) 
     
    if db_select( path ) = DB_OK then 
        return DB_OK 
    end if 
     
    if db_open( path, lock_mode ) = DB_OK then 
        return DB_OK 
    end if 
     
    return db_create( path, lock_mode ) 
end function 

-Greg

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

5. Re: Trouble with Database(EDS)

The cause of your problem is the documentation, sorry.

Here is a start of what needs to be covered about key:value pairs.

As you will see Greg has packaged these ideas into one routine.

_tom


Error Status Constants

Constant Value Meaning
DB_OK 0 Database is OK, not error has occurred
DB_OPEN_FAIL -1 The database could not be opened
DB_EXISTS_ALREADY -2 The database could not be created, it already exists
DB_LOCK_FAIL -3, A lock could not be gained on the database
DB_BAD_NAME -4 An invalid name suppled when creating a table
DB_FATAL_FAIL -404 A fatal error has occurred



Constant Value Meaning
DB_LOCK_NO 0 Do not lock the file.
DB_LOCK_SHARED 1 Open the database with read-only access but allow others to update it.
DB_LOCK_EXCLUSIVE 2 Open the database with read and write access
DB_LOCK_READ_ONLY 3 Open the database with read-only access and ignore others updating it

Key:Value

A common way to organize data is to create a sequence of Key:Value pairs. The Key is used as an index to access the Value. A Key is often unique, small, easy to remember, and easy to search. A Value may be anything, and does not have to be unique.

-- simple example of Key:Value data pairs 
 
sequence colors = { {"red",{255,0,0}}, {"green", {0,255,0}}, {"blue",{0,0,255}}, $ } 

Since the idea of Key:Value data pairs is so useful there are many ways of working with large datasets.

  • associative list
  • std/map.e
  • std/eds.e
  • [Phix] dictionary

The choices differ in convenience and speed; they all provide Key:Value pairs.

A eds database is convenient when:

  • a table has many records
  • a database has many tables
  • you have many databases
  • finaly, you want to save the database as a disk file

EDS Euphoria Database System

The eds database uses standard database language:

  • An eds database is "as collection of tables; inside a directory of files."
  • An eds table is "a collection of records ( key:data pairs); inside a database."
  • An eds record is "one key:data pair; inside a table."
  • The eds key is "an index to the record."
  • The eds data is "the cargo (value) of the record."

To work with eds you:

  • db_select) to get a database
  • db_select_table) to get a table
  • proceed to read|write records (key:value pairs)

Before you can select you must open. Before you can open you must create.

The full viewpoint of working with eds is:

  • db_create) a database
  • db_open) the database
  • db_select) the database
  • db_create_table) a table
  • db_select_table) the table
  • proceed to read|write records (key:value pairs) When finished you db_close) the database.

Note that eds has a few safeguards:

  • once you db_create) you can not create again; else you erase the database
  • once you db_create_table) you can not create again; else you erase the table

The eds database has a few one time only conveniences:

  • when you db_create) a database it is also open and selected
  • when you db_create_table) it is also selected

The eds system is designed so you can work with many databases each containing many tables. Databases are good for working with large amounts of data.

Example

include std/eds.e 
atom db_msg 
 
-- first level of thinking is "database" 
 
db_msg = db_select( "GameDatabase" ) 
 
	if db_msg = DB_OK then 
		puts(1, "GameDatabase is selected for use" ) 
	 
	else db_msg = db_open( "GameDatabase" ) 
 
		if db_msg = DB_OK then 
			puts(1, "GameDatabase opened and selected for use" ) 
		 
		else db_msg = db_create( "GameDatabase" ) 
			if db_msg = DB_OK then 
				puts(1, "GameDatabase created, opened, and selected for use" ) 
			else 
				puts(1, "Database Failure" )			 
			end if 
		end if 
	end if 
 
-- second level of thinking is "table" 
 
new topic     » goto parent     » topic index » view message » categorize

6. Re: Trouble with Database(EDS)

Thanks Tom,

I think I understand it better. Here is my updated code.

EDIT: Does anyone have an updated version of EDS viewer? Something that gives you a GUI way of looking through databases.

include std/eds.e 
include std/get.e 
include std/map.e 
 
without type_check 
 
atom db 
 
db = db_select("GameDatabase") 
 
if db = DB_OK then 
	puts(1,"Database is selected for use") 
	else db = db_open("GameDatabase") 
		if db = DB_OK then 
			puts(1,"Database open and selected for use") 
		else db = db_create("GameDatabase") 
		if db = DB_OK then 
			puts(1,"Database created, open and ready for use") 
		else 
			puts(1,"Database failure") 
		end if 
	end if 
end if 
 
atom tab 
 
tab = db_select_table("GameTable") 
 
if tab = DB_OK then 
	puts(1,"Table selected for use") 
	else tab = db_create_table("GameTable") 
	if tab = DB_OK then 
		puts(1,"Table created and selected for use") 
	else 
		puts(1,"Table failure") 
	end if 
end if 
 
db_insert("Uncharted 4",{"Naughty Dog",50}) 
new topic     » goto parent     » topic index » view message » categorize

7. Re: Trouble with Database(EDS)

Ok I got something working almost. It display 0 when it should show the correct size and I'm not sure how I would add more entries into the database.

include std/eds.e 
include std/get.e 
include std/map.e 
 
without type_check 
 
sequence GameDB = "GameDB.edb" 
sequence GameTable = "Game" 
atom game_id 
sequence game_data = repeat(0,256) 
integer idkey 
 
if db_select(GameDB) != DB_OK then 
	if db_open(GameDB,DB_LOCK_NO) != DB_OK then 
	if db_create(GameDB,DB_LOCK_NO) != DB_OK then 
				puts(1,"Could not create database!\n") 
		abort(1) 
	end if 
 end if 
 	db_select(GameDB) 
end if 
 
if db_select_table(GameTable) != DB_OK then 
	if db_create_table(GameTable) != DB_OK then 
		puts(1,"Table not created!\n") 
		abort(1) 
	end if 
	db_select_table(GameTable) 
end if 
 
game_data = {"Uncharted 4","Naughty Dog",{50},"Single Player","Multiplayer"} 
game_id = hash(game_data[1..3],GameTable) 
 
idkey = db_find_key(game_id) 
if idkey < 0 then 
	if db_insert(game_id,{game_data}) != DB_OK then 
		puts(1,"Could not insert game!\n") 
		abort(1) 
	else 
		printf(1,"Game ID: %d added.\n",game_id) 
	end if 
	else 
		game_data = db_record_data(idkey) 
		printf(1,"Game ID: %d\n",game_id) 
		if length(game_data) = 1 then 
			game_data = game_data[1] 
			printf(1,"Title: %s\n",{game_data[1]}) 
			printf(1,"Developer: %s\n",{game_data[2]}) 
			printf(1,"Size: %d\n",{game_data[3]}) --shows up as 0 currently 
			printf(1,"Single Player: %s\n",{game_data[4]}) 
			printf(1,"Multiplayer: %s\n",{game_data[5]}) 
		end if 
end if 
 
db_close() 
new topic     » goto parent     » topic index » view message » categorize

8. Re: Trouble with Database(EDS)

Icy_Viking said...
	if db_insert(game_id,{game_data}) != DB_OK then 

I'm not at my PC to test, but I think this can just be:

	if db_insert(game_id,game_data) != DB_OK then 

No sequence braces necessary, as it's already a sequence. You're making it a one-element sequence doing it that way.

I hope that helps.

Upon further viewing, I see that you account for that in the code. However, I still think it's unnecessary... maybe? grin

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

9. Re: Trouble with Database(EDS)

euphoric said...
Icy_Viking said...
	if db_insert(game_id,{game_data}) != DB_OK then 

I'm not at my PC to test, but I think this can just be:

	if db_insert(game_id,game_data) != DB_OK then 

No sequence braces necessary, as it's already a sequence. You're making it a one-element sequence doing it that way.

I hope that helps.

Upon further viewing, I see that you account for that in the code. However, I still think it's unnecessary... maybe? grin

Well I would like to add more entries into my database, so there must be a way to add more elements into the table/database.

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

10. Re: Trouble with Database(EDS)

change this one line

 
			printf(1,"Size: %d\n", game_data[3] ) --was shows up as 0 currently  
			                                       -- now 50 

_tom

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

11. Re: Trouble with Database(EDS)

_tom said...

change this one line

 
			printf(1,"Size: %d\n", game_data[3] ) --was shows up as 0 currently  
			                                       -- now 50 

_tom

Thanks Tom. Ok I mananged to add multiple entries. Is there a GUI way of viewing the database?

include std/eds.e 
include std/get.e 
include std/map.e 
 
without type_check 
 
sequence GameDB = "GameDB.edb" 
sequence GameTable = "Game" 
atom game_id 
sequence game_data  
integer idkey,idkey2 
sequence game_data2 
atom game_id2 
 
 
if db_select(GameDB) != DB_OK then 
	if db_open(GameDB,DB_LOCK_NO) != DB_OK then 
	if db_create(GameDB,DB_LOCK_NO) != DB_OK then 
				puts(1,"Could not create database!\n") 
		abort(1) 
	end if 
 end if 
 	db_select(GameDB) 
end if 
 
if db_select_table(GameTable) != DB_OK then 
	if db_create_table(GameTable) != DB_OK then 
		puts(1,"Table not created!\n") 
		abort(1) 
	end if 
	db_select_table(GameTable) 
end if 
 
game_data = {"Uncharted 4: A Thief's End","Naughty Dog",{50},"YES","YES Network 2-11"} 
game_id = hash(game_data[1..3],GameTable) 
 
game_data2 = {"Dragon's Crown Pro","Vanillware",{5.6},"YES","YES - Local/Network 2-4"} 
game_id2 = hash(game_data2[1..3],GameTable) 
 
idkey = db_find_key(game_id) 
if idkey < 0 then 
	if db_insert(game_id,{game_data}) != DB_OK then 
		puts(1,"Could not insert game!\n") 
		abort(1) 
	else 
		printf(1,"Game ID: %d added.\n",game_id) 
	end if 
	else 
		game_data = db_record_data(idkey) 
		printf(1,"Game ID: %d\n",game_id) 
		if length(game_data) = 1 then 
			game_data = game_data[1] 
			printf(1,"Title: %s\n",{game_data[1]}) 
			printf(1,"Developer: %s\n",{game_data[2]}) 
			printf(1,"Size: %f GB\n",game_data[3]) 
			printf(1,"Single Player: %s\n",{game_data[4]}) 
			printf(1,"Multiplayer: %s\n",{game_data[5]}) 
		end if 
end if 
 
puts(1,"\n") --to add space between entries 
 
idkey2 = db_find_key(game_id2) 
if idkey2 < 0 then 
	if db_insert(game_id2,{game_data2}) != DB_OK then 
		puts(1,"Could not insert game!\n") 
		abort(1) 
	else 
		printf(1,"Game ID: %d added.\n",game_id2) 
	end if 
	else 
		game_data2 = db_record_data(idkey2) 
		printf(1,"Game ID: %d\n",game_id) 
		if length(game_data2) = 1 then 
			game_data2 = game_data2[1] 
			printf(1,"Title: %s\n",{game_data2[1]}) 
			printf(1,"Developer: %s\n",{game_data2[2]}) 
			printf(1,"Size: %f GB\n",game_data2[3]) 
			printf(1,"Single Player: %s\n",{game_data2[4]}) 
			printf(1,"Multiplayer: %s\n",{game_data2[5]}) 
		end if 
end if 
 
db_close() 
new topic     » goto parent     » topic index » view message » categorize

12. Re: Trouble with Database(EDS)

Icy_Viking said...

Thanks Tom. Ok I mananged to add multiple entries. Is there a GUI way of viewing the database?

http://rapideuphoria.com/dat.htm

The Archive has a variety of eds extras.

_tom

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

13. Re: Trouble with Database(EDS)

Thanks, I found something called SequenceViewer, which gives you a GUI way of looking through Databases. I've noticed that hardly any if any database systems are exactly what I'm looking for, but oh well. At least EDS is pretty easy to learn, SQL/MySQL, something I've always had trouble with, I understand to a degree.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu