mysql 嵌入式 python实现...
时间:2010-08-15 来源:lqefn
from ctypes import *
my = windll.libmysqld
NULL = 0
mysql = c_int(0)
results = c_int(0)
record = c_int(0)
s = "MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS, MYSQL_OPT_NAMED_PIPE, MYSQL_INIT_COMMAND, MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP, MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME, MYSQL_OPT_LOCAL_INFILE, MYSQL_OPT_PROTOCOL, MYSQL_SHARED_MEMORY_BASE_NAME, MYSQL_OPT_READ_TIMEOUT, MYSQL_OPT_WRITE_TIMEOUT, MYSQL_OPT_USE_RESULT, MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION, MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH, MYSQL_REPORT_DATA_TRUNCATION, MYSQL_OPT_RECONNECT, MYSQL_OPT_SSL_VERIFY_SERVER_CERT"
l=s.split(", ")
for i, v in enumerate(l):
#print("%s = %d"%(v, i))
exec("%s = %d"%(v, i))
print("start")
server_options = (c_char_p*2)(b"mysql_test", b"--defaults-file=my.ini")
num_elements = c_int(sizeof(server_options)// sizeof(c_char_p))
def err():
print("ERR:", string_at(my.mysql_error(mysql)))
print("num_elements:", num_elements)
server_groups = (c_char_p*2)(b"EmbedMySQLServer", b"libmysqld_client")
print("init:", my.mysql_server_init(num_elements, server_options, server_groups))
err()
mysql = my.mysql_init(NULL)
err()
print("mysql:", mysql)
print("option:", my.mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, b"libmysqld_client"))
print("option:", my.mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL))
err()
print("connect:", my.mysql_real_connect(mysql, NULL,b"root", b"pass", b"mydb", 0,NULL,0))
err()
print("query:", my.mysql_query(mysql, b"set names utf8"))
print("query:", my.mysql_query(mysql, b"select * from test"))
err()
results = my.mysql_store_result(mysql)
print("results:", results)
my.mysql_fetch_row.restype = POINTER(c_char_p)
for r in range(my.mysql_num_rows(results)):
record = my.mysql_fetch_row(results)
#print("record", record)
if not record:
break
for c in range(my.mysql_num_fields(results)):
print(record[c].decode("utf8"), end = ",")
print()
my.mysql_free_result(results)
my.mysql_close(mysql)
my.mysql_server_end()
#my.ini
[EmbedMySQLServer]
basedir = D:/Program Files/MySQL/MySQL Server 5.1
datadir = D:/Program Files/MySQL/Data/Data
language = D:/Program Files/MySQL/MySQL Server 5.1/share/english
port=3306
[libmysqld_client]
language = D:/Program Files/MySQL/MySQL Server 5.1/share/english
port=3306