[help+]Does Construct access SQL Databases?

For questions about using Classic.

Post » Sun May 15, 2011 7:58 pm

[quote="scidave":2nokla3v]Either way works fine...I've done both. As long as your version of Python is 2.6..it isn't a problem to replace the current files with the ones you copy (but it is not necessary..just select "no" to overwriting existing files.[/quote:2nokla3v]
thanks a lot for helping ;)

I will see if now I can access SQL databases
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon May 16, 2011 6:46 am

@Scidave

hey dude, do u know why python can't get the info from EditBox.Text ?
the '.Text' is appearing in red at the right bar.. is that okay? What means the red color?

i'm trying this:

[code:kxdkm6nk]import MySQLdb

db = MySQLdb.connect("localhost","root","","TESTING" )

cursor = db.cursor()

sql = """INSERT INTO TEST(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES (EditBox.Text, EditBox2.Text, 20, 'M', 2000)"""

try:

cursor.execute(sql)
db.commit()
except:
db.rollback()[/code:kxdkm6nk]
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue May 17, 2011 6:29 am

Now I've tryed add

name = str(EditBox.Text) before sql instruction and change:
[code:1mua2ebh]sql = """INSERT INTO TEST(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES (EditBox.Text, EditBox2.Text, 20, 'M', 2000)"""[/code:1mua2ebh]

to:
[code:1mua2ebh]sql = """INSERT INTO TEST(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES (name, 'test', 20, 'M', 2000)"""[/code:1mua2ebh]

but still nothing.. doesn't add nothing to the database, It's like the script can't read the text from editbox..
If I change VALUES (name, 'test', 20, 'M', 2000) to VALUES ('nametest', 'test', 20, 'M', 2000) then it works.. but I need to make it add what is written in the editboxes..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Wed May 18, 2011 1:05 am

Try this:

[code:3c66z9fl]

sql = """INSERT INTO TEST(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('%s', '%s', 20, 'M', 2000)"""

try:

cursor.execute(sql,EditBox.Text,EditBox2.Text)
db.commit()
except:
db.rollback()


[/code:3c66z9fl]

If that doesn't work you can try changing the cursor execute line to this (notice the commas):
cursor.execute(sql,(EditBox.Text,),(EditBox2.Text,))
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Wed May 18, 2011 2:02 am

[quote="scidave":30en8u9a]Try this:

[code:30en8u9a]

sql = """INSERT INTO TEST(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('%s', '%s', 20, 'M', 2000)"""

try:

cursor.execute(sql,EditBox.Text,EditBox2.Text)
db.commit()
except:
db.rollback()


[/code:30en8u9a]

If that doesn't work you can try changing the cursor execute line to this (notice the commas):
cursor.execute(sql,(EditBox.Text,),(EditBox2.Text,))[/quote:30en8u9a]

I've tryed both ways with and without commas but none worked.. =(
any more ideas?
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Wed May 18, 2011 11:30 am

Did you also try setting the EditBox.Text to python variables with the new way?

name1 = EditBox.Text
name2 = EditBox2.Text

Then replace those names in the execute call.

Then if that fails, try it without the single quotes around the %s....

If none of those work, I'll download/install the library and see what's up.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Wed May 18, 2011 4:26 pm

[quote="scidave":yv220wy5]Did you also try setting the EditBox.Text to python variables with the new way?

name1 = EditBox.Text
name2 = EditBox2.Text

Then replace those names in the execute call.

Then if that fails, try it without the single quotes around the %s....

If none of those work, I'll download/install the library and see what's up.[/quote:yv220wy5]

Ya I had already tryed those too and now tryed without the quotes around %s but don't worked too..
If you could download them I would greatly appreciate your effort.. I'm getting crazy trying to make this work
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Thu May 19, 2011 12:30 am

Got it! :D I installed everything and experimented with the database and found this portion of the user guide helpful:
http://mysql-python.sourceforge.net/MyS ... on-objects

[code:2pt4equn]
firstname = 'sci'
lastname = 'dave'
age = 20
sex = "M"
income = 2000

sql = """INSERT INTO test(FIRST_NAME, LAST_NAME,
AGE, SEX, INCOME) VALUES (%s, %s, %s, %s, %s)"""

try:

cursor.execute(sql , (firstname,lastname,age,sex,income))
db.commit()
except:
db.rollback()

[/code:2pt4equn]

You could also hardcode values you want as well. Also, notice that whether you are placing a string or int you always use a "%s" for them as the placeholder.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Thu May 19, 2011 1:59 am

OMG Thank you!!!!! I'm gonna try this!


Edit: Wowww it really worked! Can't thank you enough for this... I was getting crazy making this to work..

once more, thanks!

I'm trying to read from the DB with this script, not getting any error, just doesn't showing nothing:
[code:yd0jhkoc]
maxprice = 1000

cursor.execute("""SELECT FIRST_NAME FROM TEST
WHERE INCOME > %s""", (maxprice))


results = cursor.fetchone()
EditBox3.AppendText(results)[/code:yd0jhkoc]
Do you know what might be the problem?

EDIT2: Ok, I fixed I guess..
changed EditBox3.AppendText(results) to EditBox3.AppendText(str(results)) :D
I think this topic serves as a tutorial too.. xD
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon May 23, 2011 10:57 pm

Hey Scidave or any other

I'm with a new problem here..

I've made a simple construct game with some of those scripts so my friend could test if he can access my database and add/read from it...
I've changed MySQLdb.connect in inital script to:

db = MySQLdb.connect("MY GLOBAL IP","root","","highscores" )
and I compiled everything selecting the pyc files.
When he tries to test the game, appear an error where it says "host 'his global ip' is not allowedto connect to this MySQL server"
so probably i had to change something to allow remote access to my db.. so I looked for help in google and found that i need to add in my.cnf
bind-address=my_ip and add a comment to skip-networking.
I'm using XAMPP so i've found this file at the mysql folder and the skip-networking was already commented and I added the bind-address=my_localip
but he still had the same error.. so probably it not worked or I'm doing something wrong.. any1 know what it should be??
thanks..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

PreviousNext

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 9 guests