Created
October 26, 2022 12:31
-
-
Save DxDiagDx/c93261188346f94f3f5581955a5d111c to your computer and use it in GitHub Desktop.
WooCommerce - MySQL - Python: получить информацию о товарах из базы данных
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sshtunnel import SSHTunnelForwarder | |
import pymysql | |
host = 'test.beget.tech' | |
login = 'test' | |
password = 'test' | |
user_db = 'test_db' | |
password_db = 'test' | |
name_db = 'test_db' | |
def main(): | |
server = SSHTunnelForwarder( | |
ssh_address_or_host=host, | |
ssh_username=login, | |
ssh_password=password, | |
remote_bind_address=('127.0.0.1', 3306) | |
) | |
server.start() | |
con = pymysql.connect( | |
host='127.0.0.1', | |
port=server.local_bind_port, | |
user=user_db, | |
password=password_db, | |
db=name_db | |
) | |
with con: | |
cur = con.cursor() | |
cur.execute(""" | |
SELECT p.ID, | |
p.post_title, | |
IF (meta.meta_key = '_sku', meta.meta_value, null) | |
FROM wp_posts AS p | |
LEFT JOIN wp_postmeta AS meta | |
ON p.ID = meta.post_ID | |
WHERE meta.meta_key = '_sku' | |
LIMIT 1 | |
""") | |
products = cur.fetchone() | |
print(products) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment