SQLite Inserting, Deleting and stuff
#1

Hi guys, I have a problem which I don't know how to fix.

I am using SQLite for my business system.

With my command, I am inserting a biz like this
Код:
new ID = SpawnedBiz+1;
...
...
format(Query,sizeof(Query),"INSERT INTO `firme` (`IDFirme`,`ImeVlasnika`,`Vrsta`,`KolicinaGorivaNaPumpi`,`NovacUFirmi`,`Cijena`,`EXTX`,`EXTY`,`EXTZ`,`Owned`) VALUES ('%d','Nitko','%d','50','0','%d','%f','%f','%f',0)",ID,Type,cijena,xa,ya,za);
db_query( Database, Query );
SpawnedBiz++;
...
As you can see, the very first one biz will have an ID 1 and then 2 , 3 and so on...

*This is how my Database looks like*

I am using ">>>" only for spaces

>>>>>>|BIZ ID| |OWNER NAME | |TYPE OF BIZ | |ETC|
Row 1>>> 1>>>>>>> Bob>>>>>>>> 1
Row 2>>> 2>>>>>>> John>>>>>>> 1
Row 3>>> 3>>>>>>> Joe>>>>>>>> 3
Row 4>>> 4>>>>>>> Nancy>>>>>> 3
Row 5>>> 5>>>>>>> Alex>>>>>>>> 2


Now, what if I want to delete a row where BIZ ID 3?
Now it looks like this right?

>>>>>>|BIZ ID| |OWNER NAME | |TYPE OF BIZ | |ETC|
Row 1>>> 1>>>>>>> Bob>>>>>>>> 1
Row 2>>> 2>>>>>>> John>>>>>>> 1
Row 3>>> 4>>>>>>> Nancy>>>>>> 3
Row 4>>> 5>>>>>>> Alex>>>>>>>> 2

And now my question is: When I create a biz next time, it will have an ID of 6 and will be inserted after Row 4. How to make it so it creates an ID of 3 and puts it where it belongs, between Row 2 and Row 4?

I hope you understand what I mean.

Regards
Reply
#2

You can use for deleting
pawn Код:
UPDATE `firme` SET `owner_name` = 'N/N' WHERE `IDFirme` = 3
then for inserting
pawn Код:
new rowid = 0;
SELECT `IDFirme` FROM `firme` WHERE `owner_name` = 'N/N' LIMIT 1
if(num_rows)
{
rowid = get field 0 for storing IDFirme
}
free result

if(rowid > 0)
{
UPDATE `firme` SET ... WHERE IDFirme = rowid
}
else
{
INSERT INTO `firme` SpawnedBiz
SpawnedBiz++;
}
and in loading biz

pawn Код:
SELECT * FROM `firme` WHERE `owner_name` != 'N/N'
Reply
#3

Can I do something like this -

When I execute the command for deleting a business, it will only erase fields in that row and make them all NULL.
Now when I want to create a business, can it Select a row where , let's say, "IDFirme(Biz ID in english)" is NULL?

So, when I am in process of creating, there are two options:
1.) IF the script finds NULL under "IDFirme" it will just UPDATE the existing one and set default, fresh business values.
2.) ELSE if everything is okay and not NULL, it will just to the thing I already wrote.

Can you , sir, please help me with that?

Regards
Reply
#4

Sorry for DP but I made something, correct me if I am wrong.

Creating the biz

//FirmaInfo[rowid][IDFirme] = strval(Field); - I put that in comment because I don't know should I attach it to a variable. Look at the codes below

Код:
if(vrsta == 1)
			{
			    
				GetPlayerPos(playerid,xa,ya,za);
				FirmaInfo[ID][EXTX] = xa;
				FirmaInfo[ID][EXTY] = ya;
				FirmaInfo[ID][EXTZ] = za;
				FirmaInfo[ID][Cijena] = cijena;
				FirmaInfo[ID][Vrsta] = vrsta;
				
				format(Query,sizeof(Query),"SELECT * FROM `firme` WHERE `IDFirme` = '0' LIMIT 1");
				Result = db_query( Database, Query );
				if( db_num_rows( Result ) )
				{
					rowid = db_get_field_assoc( Result, "IDFirme", Field, 4 );
					//FirmaInfo[idfirme][IDFirme] = strval(Field);
				}
				db_free_result( Result );
				if(rowid > 0)
				{
					format(Query,sizeof(Query),"UPDATE `firme` SET `IDFirme`= '%d',`ImeVlasnika` = 'Nitko',`Vrsta` = '%d',`KolicinaGorivaNaPumpi` = '50',`NovacUFirmi` = '0',`Cijena` = '0',`EXTX` = '%f',`EXTY` = '%f',`EXTZ` = '%f',`Owned` = '0' WHERE `IDFirme` = '%d'",rowid, vrsta,cijena,xa,ya,za,rowid);
	                db_query( Database, Query );
				}
				else
				{
					
					format(Query,sizeof(Query),"INSERT INTO `firme` (`IDFirme`,`ImeVlasnika`,`Vrsta`,`KolicinaGorivaNaPumpi`,`NovacUFirmi`,`Cijena`,`EXTX`,`EXTY`,`EXTZ`,`Owned`) VALUES ('%d','Nitko','%d','50','0','%d','%f','%f','%f',0)",ID,vrsta,cijena,xa,ya,za);
                	db_query( Database, Query );
                	SpawnanihFirmi++;
                	format(FirmaString,sizeof(FirmaString),"{00C0FF}Firma na prodaju{FFFFFF} \n{00C0FF}Vrsta:{FFFFFF} Benzinska Pumpa\n{00C0FF}Cijena: {FFFFFF}%d$\n{48E31C}/kupifirmu",cijena);
			 		FirmaInfo[ID][Label] = CreateDynamic3DTextLabel(FirmaString, 0xFFFFFFFF, FirmaInfo[ID][EXTX], FirmaInfo[ID][EXTY], FirmaInfo[ID][EXTZ], 30.0);
	 				FirmaInfo[ID][Pickup] =  CreateDynamicPickup(1212, 1, FirmaInfo[ID][EXTX], FirmaInfo[ID][EXTY], FirmaInfo[ID][EXTZ], 0);
                }
			}
For deleting I am sure its OK because it only UPDATES some values to zero and if Biz ID is 0, it won't load and that's it.
Reply
#5

It's not working. Anybody else?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)