Challenge: Connecting to a MySQL Database and Executing Commands
メニューを表示するにはスワイプしてください
This task will recap most of the basic concepts of this chapter but the solution for it is very similar to what we had been practicing in the videos. Your task is to:
- Write the code for connecting to a server. You can connect to your local server if you have it hosted on your PC. If not, you can enter placeholder values in the connection string. The database name in the connection string should be
school; - Make sure you are correctly closing the connection after use; Use either the
Close()method or theusingstatement; - Enclose the relevant code into a try-catch block;
- Execute the command for creating a new table called
teachers, which will have four columns, namelyid,name,age, andexperience. Use the following command:CREATE TABLE teachers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), age INT, experience FLOAT); - Execute the following three commands, which will insert 3 rows into the database:
INSERT INTO teachers (name, age, experience) VALUES ('John Smith', 35, 5.5);
INSERT INTO teachers (name, age, experience) VALUES ('Anna Johnson', 40, 8.2);
INSERT INTO teachers (name, age, experience) VALUES ('Robert Davis', 32, 3.1);
- Finally, read and display the data of the 3 rows. The column
nameis of typeString,ageis of typeInt32, andexperienceis of typeFloat.
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 9
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 9