Help - using databases

Discuss PocketC for Palm OS (including Desktop Edition)

Postby wossname on Tue May 08, 2001 3:47 pm

I need to be able to store 40 integers in a database called "FT Score", and then retrieve each of them in turn later on.

I haven't got any code at the moment because I had a bit of a fit and deleted it!

Could you give me some simple code to be able to write these 40 integers from an array into the database, and then later on read them out again into the same array.

I'm using version 4.0.3 of PocketC.

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am

Postby awschwab on Thu May 10, 2001 6:33 pm

Try this, and compare timings of several methods.

Alan

//DBDemo.pc

string makeDBfmt(string sFmt, int length);

start(pointer pt)
{
*pt = ticks();
}
stop(string s, pointer pt)
{
puts(s + ": " + (ticks() - *pt) + " tics\n");
}


main()
{
int i, length = 40, t;
string sFilename = "FT score", sFormat;
string sDatType = "i4"; // replace this with the data type for your DB
// "i4", "i2", "c" are 32, 16, 8 bit integer types.

// int data[40], output[40];
// arrays are dynamically allocated to allow variation of length above.
pointer data, output;
data = malloct(length, "i");
output = malloct(length, "i");

// fake some data
for (i = 0; i < length; i++)
{
data[i] = i;
}

start(&t);
if(!dbcreatex(sFilename,"PktC","user"))
// vary creator and type, but they should be 4 char strings.
// Only "PktC", "user" DBs will show up in PocketC DB list.
// If you use a different DB type, you should write a function to delete it.
// dbopen is faster than dbcreate, but it does not clear the old DB, and is less safe.
{
alert("Database creation failed!\n");
dbclose(); // neccesary?
return(1);
// dbcreatex will fail if there is an existing DB of the
// name specified, with a different owner or type.
}
dbclose();
stop("DB created", &t);

start(&t);
dbopen(sFilename);
i = 0;
while (i < length)
dbwrite(*(output + i++)); // writes int as "i4"
dbclose();
stop("DB written using dbwrite()", &t);


start(&t);
dbopen(sFilename);
i = 0;
while (i < length)
*(output + i++) = dbread("i"); //equivalent to: output[i++] = dbread("i");
dbclose();
stop("DB read using dbread()", &t);


start(&t);
sFormat = makeDBfmt(sDatType, length);
stop("Format string made", &t);

start(&t);
if(!dbcreatex(sFilename,"PktC","user"))
{
alert("Database creation failed!\n");
dbclose();
return(1);
}
dbclose();
stop("DB wiped", &t);

start(&t);
dbopen(sFilename);
dbwritex(data, sFormat);
dbclose();
stop("DB written using dbwritex()", &t);

start(&t);
dbopen(sFilename);
dbreadx(output, sFormat);
dbclose();
stop("DB read using dbreadx()", &t);


// Show that both arrays are the same:
puts("First array:\n");
if (length > 30)
i = length - 30; // just show last 30 pts
else i = 0;
while(i < length)
{
puts(data[i++] + " ");
}
puts("\nSecond array:\n");
if (length > 30)
i = length - 30;
else i = 0;
while(i < length)
{
puts(output[i++] + " ");
}
puts("\n");


return(0);
}


string makeDBfmt(string sFmt, int length)
{
int fmtLen = 1, size;

size = length * strlen(sFmt);
while (fmtLen < length)
{
sFmt = sFmt + sFmt;
fmtLen = fmtLen << 1;
}
return(strleft(sFmt, size));
}




Edited by - awschwab on 05/10/2001 13:51:56
awschwab
 
Posts: 270
Joined: Tue Feb 06, 2001 2:33 pm
Location: USA

Postby wossname on Fri May 11, 2001 3:19 pm

Thanks to all the people that have helped me with this thread. I've sorted the problem now, all I need is some people that are interested in Field Target Shooting!

Thanks again.

Adam Ward.

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am

Postby soinsg on Sat May 19, 2001 2:05 pm

Any shooting game is good! I have no idea what field shooting is? Please email me the completed source.

--
soinsg@hotmail.com
soinsg
 
Posts: 198
Joined: Thu May 17, 2001 4:26 pm

Postby wossname on Sat May 19, 2001 3:53 pm

I'm sorry it is not a game, its more like a program that lets you keep track of your progress as you move from target to target. Much like something a golfer would take to the course, and keep scores for his group, so they can draft it up properly on the card later at the 19th hole.

Didn't mean to get your hopes up!

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am

Postby gjperker on Mon May 21, 2001 11:50 am

Shooting !?! Hey, never thought that would come up in this forum. I shoot trap, pistol and rifle when I get the chance. What have we got going?

Greg Perkowski
greg@perkowski.net
Greg Perkowski
greg@perkowski.net

The code is out there.
Follow the code Luke....
Beam me up, there's no intelligent code here.
gjperker
 
Posts: 8
Joined: Fri May 04, 2001 12:34 am
Location: USA

Postby wossname on Mon May 21, 2001 3:16 pm

Well, the shooting I do is less noisy than those!

My club is called "Castle Field-Target Air-Rifle Club). See http://www.Castleftarc.com for our homepage (made by yours truly!).

I have 2 rifles, one is of "Spring" type (believe it or not it actually recoils more than a .30 cal centrefire rifle!) and the other is pneumatic type (it's reservoir is filled from a diver's aqualung to a standard working pressure of 2700psi!

Both guns are rated at 11.94 ft/lbs (12 is the maximum legal level without requiring a license).

We shoot over a 20 target course (stationary targets) in a picturesque coppice in England. 2 shots at each target, if you hit the red circle in the middle of the target, the whole thing falls over and you get a full 10 points. If you hit anywhere around the circle (kill-zone) you get 5 points, a complete miss gets you 0 points. Each target is set up at ranges of between 7 and 65 yards, anywhere in the forest, including high up in trees, under bits of debris etc...

The targets are shaped like typical forest animals, crows/rabbits/squirrels and so on.

Please note that we don't shoot living creatures, to do so would mean instant dismissal from the club and being reported to the wildlife commission!!!

So there you go, take a look at the URL above, and drop us a line! :)

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am


Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 4 guests

cron