[help+]Does Construct access SQL Databases?

For questions about using Classic.

Post » Sun May 29, 2011 11:53 pm

[quote="Guif0DA":1z5hvyx0]This while(1) from the first code means what?
it's true?[/quote:1z5hvyx0]
It means loop forever.

[quote="Guif0DA":1z5hvyx0]instead of print I should use SetText right?[/quote:1z5hvyx0]
Correct. I was just showing an example you could test in straight Python.

[quote="Guif0DA":1z5hvyx0]Also, what is the difference retrieve as python dictionaries? didn't understood what u meant by index..[/quote:1z5hvyx0]
If the result is return as a Tuple (a List) and you just print out the Tuple you will get a funky looking "('TESTER,')" <----a Tuple with only one entry. Instead if you said row[0], that would just return "TESTER" as it is the first element/index in the row.

All of this is just a guess as I actually haven't tried this out in code..don't have my computer with the database on it with me right now...I might be wrong on some of this.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon May 30, 2011 2:44 am

Hey Scidave, I think it worked, here is the code:

[code:3ukj37gj]
user=username.Text

query4="""SELECT STATUS FROM ACCOUNTS WHERE USERNAME = %s"""
cursor.execute(query4,(user))

results=cursor.fetchall()
for row in results:
value = row[0]

Text12.SetText(str(value))
[/code:3ukj37gj]

but it doesn't work with fetchone(), only fetchall..probably because of 'for'.. don't know.. Could I get the same result using fetchone() ?
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon May 30, 2011 3:15 am

You can use fetchone() to get the rows one at a time, or fetchall() to get them all at once.

So it is just a difference in syntax. If you had a massive databse then to save on memory you might want to use fetchone(), but in your case it doesn't practically matter which one you use.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon Jun 13, 2011 10:38 pm

Hey ;D

this time it's more about a curiosity than a question..
how do I make many Insertions on the database at the same time?
Right now the method I'm using is this:
[code:1d3cl0fd]
sql1="INSERT"
cursor.execute(sql1)
sql2="INSERT"
cursor.execute(sql2)
sql...="INSERT"
cursor.execute(sql...)[/code:1d3cl0fd]

Can I insert everything I want at the sime time, without the need to use execute many times in one code?
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue Jun 14, 2011 12:18 am

sqlite with Python has an executescript() function. The best the mysqldb has is a executemany() function (simple example below):

cursor.executemany(
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)
VALUES (%s, %s, %s, %s, %s)""",
[
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
] )

This link may be useful as well for showing more complexity:
http://stackoverflow.com/questions/9747 ... xecutemany
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Tue Jun 14, 2011 3:12 am

That's really great and now that u said about executemany I remember I already readed about it.. but at least now I know how to use ;D
so.. as always, thanks Scidave!
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Sat Jun 25, 2011 7:08 am

Scidave do u know how I can make to delete only 1 row with a DELETE querie?

Right now it's deleting all the rows that match the conditions but I just need to delete one of them.. how can I achieve that?

I've looked into some of the tutorials but haven't found nothing.. where could I look for help instead of python.org?

edit: nevermind... Found it.. LIMIT 1 ;)
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Previous

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests