Python, insert record into database from JOIN statement
up vote
2
down vote
favorite
I'm trying to write combined table data into a new table for a timing system I'm working on.
The following SQL works in PHPMyAdmin.:
INSERT
INTO
results(
firstName,
lastName,
raceNumber,
raceTime
)
SELECT
s.firstName,
s.lastName,
s.raceNumber,
h.time
FROM
runners s
INNER JOIN
chipData hp
ON
s.raceNumber = hp.bandID
INNER JOIN
readings h
ON
hp.tagId = h.tagId
WHERE
hp.tagId = 123456
LIMIT 1
However, if I add this into a Python statement as follows, it doesn't work:
db = connect()
cur = db.cursor()
cur.execute("""INSERT INTO results( firstName, lastName, raceNumber, raceTime ) SELECT s.firstName, s.lastName, s.raceNumber, h.time FROM runners s INNER JOIN chipData hp ON s.raceNumber = hp.bandID INNER JOIN readings h ON hp.tagId = h.tagId WHERE hp.tagId = %s LIMIT 1""", (123456)
db.commit()
db.close()
Any help is appreciated!
python sql database join
add a comment |
up vote
2
down vote
favorite
I'm trying to write combined table data into a new table for a timing system I'm working on.
The following SQL works in PHPMyAdmin.:
INSERT
INTO
results(
firstName,
lastName,
raceNumber,
raceTime
)
SELECT
s.firstName,
s.lastName,
s.raceNumber,
h.time
FROM
runners s
INNER JOIN
chipData hp
ON
s.raceNumber = hp.bandID
INNER JOIN
readings h
ON
hp.tagId = h.tagId
WHERE
hp.tagId = 123456
LIMIT 1
However, if I add this into a Python statement as follows, it doesn't work:
db = connect()
cur = db.cursor()
cur.execute("""INSERT INTO results( firstName, lastName, raceNumber, raceTime ) SELECT s.firstName, s.lastName, s.raceNumber, h.time FROM runners s INNER JOIN chipData hp ON s.raceNumber = hp.bandID INNER JOIN readings h ON hp.tagId = h.tagId WHERE hp.tagId = %s LIMIT 1""", (123456)
db.commit()
db.close()
Any help is appreciated!
python sql database join
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to write combined table data into a new table for a timing system I'm working on.
The following SQL works in PHPMyAdmin.:
INSERT
INTO
results(
firstName,
lastName,
raceNumber,
raceTime
)
SELECT
s.firstName,
s.lastName,
s.raceNumber,
h.time
FROM
runners s
INNER JOIN
chipData hp
ON
s.raceNumber = hp.bandID
INNER JOIN
readings h
ON
hp.tagId = h.tagId
WHERE
hp.tagId = 123456
LIMIT 1
However, if I add this into a Python statement as follows, it doesn't work:
db = connect()
cur = db.cursor()
cur.execute("""INSERT INTO results( firstName, lastName, raceNumber, raceTime ) SELECT s.firstName, s.lastName, s.raceNumber, h.time FROM runners s INNER JOIN chipData hp ON s.raceNumber = hp.bandID INNER JOIN readings h ON hp.tagId = h.tagId WHERE hp.tagId = %s LIMIT 1""", (123456)
db.commit()
db.close()
Any help is appreciated!
python sql database join
I'm trying to write combined table data into a new table for a timing system I'm working on.
The following SQL works in PHPMyAdmin.:
INSERT
INTO
results(
firstName,
lastName,
raceNumber,
raceTime
)
SELECT
s.firstName,
s.lastName,
s.raceNumber,
h.time
FROM
runners s
INNER JOIN
chipData hp
ON
s.raceNumber = hp.bandID
INNER JOIN
readings h
ON
hp.tagId = h.tagId
WHERE
hp.tagId = 123456
LIMIT 1
However, if I add this into a Python statement as follows, it doesn't work:
db = connect()
cur = db.cursor()
cur.execute("""INSERT INTO results( firstName, lastName, raceNumber, raceTime ) SELECT s.firstName, s.lastName, s.raceNumber, h.time FROM runners s INNER JOIN chipData hp ON s.raceNumber = hp.bandID INNER JOIN readings h ON hp.tagId = h.tagId WHERE hp.tagId = %s LIMIT 1""", (123456)
db.commit()
db.close()
Any help is appreciated!
python sql database join
python sql database join
edited Nov 13 at 22:42
Joel
1,5966719
1,5966719
asked Nov 13 at 22:12
plumby101
334
334
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14
add a comment |
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290293%2fpython-insert-record-into-database-from-join-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry, close bracket is at the end of the execute statement.
– plumby101
Nov 13 at 22:14