MySQL是一款開源的關系型數據庫管理系統,它支持多種API,比如C API、C++ API、Java API、Python API等等。不同的API適用于不同的開發環境,開發者可以選擇最適合自己的API。
C API示例代碼: #include#include main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = "password"; char *database = "mydb"; conn = mysql_init(NULL); if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } if (mysql_query(conn, "SELECT * FROM mytable")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); while ((row = mysql_fetch_row(res)) != NULL) { printf("%s %s %s\n", row[0], row[1], row[2]); } mysql_free_result(res); mysql_close(conn); }
C++ API示例代碼:
#include#include using namespace std; int main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = "password"; char *database = "mydb"; conn = mysql_init(NULL); if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { cout<< mysql_error(conn)<< endl; return 1; } if (mysql_query(conn, "SELECT * FROM mytable")) { cout<< mysql_error(conn)<< endl; return 1; } res = mysql_use_result(conn); while ((row = mysql_fetch_row(res)) != NULL) { cout<< row[0]<< " "<< row[1]<< " "<< row[2]<< endl; } mysql_free_result(res); mysql_close(conn); return 0; }
Java API示例代碼:
import java.sql.*; public class MySQLExample { public static void main(String[] args) { Connection conn; Statement st; ResultSet rs; String url = "jdbc:mysql://localhost/mydb"; String user = "root"; String password = "password"; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); st = conn.createStatement(); rs = st.executeQuery("SELECT * FROM mytable"); while (rs.next()) { System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3)); } rs.close(); st.close(); conn.close(); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } } }
Python API示例代碼:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", password="password", database="mydb" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM mytable") myresult = mycursor.fetchall() for x in myresult: print(x[0], x[1], x[2])
以上示例代碼僅供參考,開發者可以根據自己的需求進行適當修改。
上一篇css布局順序
下一篇CSS布局設計素描素材