intmain(int argc, char **argv) { MYSQL *con = mysql_init(NULL);
if (con == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); exit(1); }
if (mysql_real_connect(con, "192.168.0.204", "proxysql", "proxypassword", "testdb", 0, NULL, 0) == NULL) { finish_with_error(con); } if (mysql_query(con, "DROP TABLE IF EXISTS cars;")) { finish_with_error(con); } if (mysql_query(con, "DROP TABLE IF EXISTS people;")) { finish_with_error(con); } if (mysql_query(con, "CREATE TABLE cars(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), price INT)")) { finish_with_error(con); } if (mysql_query(con, "CREATE TABLE people(id INT, name VARCHAR(255), car_id INT)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(1,'Audi',52642)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(2,'Mercedes',57127)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(3,'Skoda',9000)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(4,'Volvo',29000)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(5,'Bentley',350000)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(6,'Citroen',21000)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(7,'Hummer',41400)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO cars VALUES(8,'Volkswagen',21600)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO people VALUES(1,'Jim',7)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO people VALUES(1,'Jim',8)")) { finish_with_error(con); } if (mysql_query(con, "INSERT INTO people VALUES(2,'Tom',6)")) { finish_with_error(con); } if (mysql_query(con, "SELECT * FROM cars")) { finish_with_error(con); } MYSQL_RES *result = mysql_store_result(con); if (result == NULL) { finish_with_error(con); } int num_fields = mysql_num_fields(result); MYSQL_ROW row; while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); }
printf("\n"); } mysql_free_result(result); if (mysql_query(con, "update cars set price = 42400 where name = 'Hummer'")) { finish_with_error(con); } if (mysql_query(con, "SELECT people.name,cars.name,cars.price FROM cars,people where cars.id = people.car_id")) { finish_with_error(con); } result = mysql_store_result(con); if (result == NULL) { finish_with_error(con); } num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); }
printf("\n"); } mysql_free_result(result); if (mysql_query(con, "delete from people where id = 1")) { finish_with_error(con); } if (mysql_query(con, "drop table people;")) { finish_with_error(con); } if (mysql_query(con, "drop table cars;")) { finish_with_error(con); } mysql_close(con); exit(0); }
publicstaticvoidmain( String[] args )throws SQLException { //option "pool" must be set to indicate that pool has to be used String connectionString = "jdbc:mariadb://192.168.0.204/testdb?user=proxysql&password=proxypassword&maxPoolSize=10&pool";
try (Connection connection = DriverManager.getConnection(connectionString)) { try (Statement stmt = connection.createStatement()) { ResultSet rs = stmt.executeQuery("DROP TABLE IF EXISTS cars;"); stmt.executeQuery("CREATE TABLE cars(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), price INT)"); stmt.executeQuery("INSERT INTO cars VALUES(1,'Audi',52642)"); stmt.executeQuery("INSERT INTO cars VALUES(2,'Mercedes',57127)"); rs = stmt.executeQuery("SELECT * FROM cars"); rs.next(); System.out.println(rs.getString(2)); } }
try (Connection connection = DriverManager.getConnection(connectionString)) { try (Statement stmt = connection.createStatement()) { stmt.executeQuery("update cars set name = 'VolksWagen' where id = 1"); stmt.executeQuery("delete from cars where id = 2"); ResultSet rs = stmt.executeQuery("SELECT * FROM cars"); rs.next(); System.out.println(rs.getString(2)); } } } }
rows, err := DB.Query("SELECT * from cars") if err != nil{ fmt.Println("query fail") } //循环读取结果 for rows.Next(){ var id int var name string var price int err := rows.Scan(&id, &name, &price) if err != nil { fmt.Println("rows fail") } fmt.Printf("%v %q %v \n", id, name, price) } }