WEBVTT 00:00:06.680 --> 00:00:13.670 It this video we're going to learn how to connect to a server and create a database using BHB. 00:00:13.680 --> 00:00:16.270 So first of all it goes to our fi FGP. 00:00:16.800 --> 00:00:25.350 Let's make sure we back up our previous file to our computer and Nola's creates a new file and coleus 00:00:25.350 --> 00:00:30.150 creates database goes page. 00:00:31.010 --> 00:00:32.940 OK so if we open this file 00:00:36.180 --> 00:00:38.700 we can impose the content from the previous one 00:00:42.020 --> 00:00:51.740 and just change the title to create a database on the head as well to create a database. 00:00:52.950 --> 00:00:56.610 And also just empty the page B.Tech for now. 00:00:57.870 --> 00:01:05.530 OK so let's access this file from our Escorial folder. 00:01:06.510 --> 00:01:13.350 OK so now before creating the database we are going to connect to the server first. 00:01:13.560 --> 00:01:20.290 So let's just of an ace a three saying connect to server. 00:01:21.020 --> 00:01:21.500 OK. 00:01:21.600 --> 00:01:28.670 So you connect to a server for all the same way folk are connecting to a database. 00:01:28.800 --> 00:01:36.420 The only difference is that rather than giving the host username password and database name to the school 00:01:36.420 --> 00:01:40.030 like next function we're going to use only the first three promises. 00:01:40.270 --> 00:01:41.490 OK. 00:01:42.360 --> 00:01:45.180 So let's just copy this piece of code 00:01:48.170 --> 00:01:51.310 because is copy the whole the whole thing. 00:01:52.170 --> 00:01:55.720 And we look basically there. 00:01:56.180 --> 00:01:56.590 OK. 00:01:56.610 --> 00:02:02.780 And let's just uncomment all this so we can find a link variable. 00:02:02.990 --> 00:02:03.860 OK. 00:02:03.920 --> 00:02:10.090 We're going to use the my as Karoli connects function and as I say rather than using four performances 00:02:10.140 --> 00:02:16.000 we're going to use only the first three properties we don't need the database thing. 00:02:16.410 --> 00:02:26.790 So we're going to attempt a connection if the connection is successful then this is going to return 00:02:27.640 --> 00:02:28.420 and objects. 00:02:28.560 --> 00:02:33.420 OK and then we're going to echo the success message. 00:02:33.420 --> 00:02:38.760 If it's not successful then this is going to be a boolean false. 00:02:38.930 --> 00:02:39.890 OK. 00:02:40.440 --> 00:02:46.410 And then we're going to have the as was of the dight function and then the BHB script is going to stop 00:02:46.410 --> 00:02:47.210 here. 00:02:47.820 --> 00:02:48.900 All right. 00:02:48.900 --> 00:02:57.360 So let's just change the access message saying connect successfully to the cell. 00:02:57.990 --> 00:02:58.420 OK. 00:02:58.680 --> 00:03:05.260 So if we save and refresh we can see that we manage to connect successfully to the server. 00:03:05.510 --> 00:03:06.750 OK. 00:03:06.910 --> 00:03:11.170 And we can see that's our link variable is objects. 00:03:11.220 --> 00:03:16.760 Let's change some of these parameters so that we get an error. 00:03:16.770 --> 00:03:17.670 So if you refresh 00:03:22.740 --> 00:03:28.590 now we can see the error message there which is the outputs of our bi function. 00:03:29.500 --> 00:03:30.050 OK. 00:03:30.060 --> 00:03:32.300 And you can see here the string the error string. 00:03:32.300 --> 00:03:35.530 It cannot connect to my escadrille server. 00:03:35.700 --> 00:03:35.940 OK. 00:03:35.940 --> 00:03:37.860 And not database. 00:03:38.700 --> 00:03:41.400 OK so that's how we connect to a server. 00:03:41.400 --> 00:03:44.540 Let's go back to our correct value. 00:03:45.440 --> 00:03:48.650 And you can see those we are connected successfully to the server. 00:03:49.110 --> 00:03:51.580 Now let's add a database. 00:03:51.600 --> 00:03:59.710 OK so let's add a new ace 3. 00:03:59.940 --> 00:04:03.720 Create a database 00:04:06.690 --> 00:04:09.700 and it creates a page B.Tech 00:04:13.030 --> 00:04:15.560 or it's 00:04:18.410 --> 00:04:23.250 OK to create the database you're gonna need to write an essay will query. 00:04:23.270 --> 00:04:27.210 OK so the SQL query is going to store. 00:04:27.480 --> 00:04:31.520 We're going to source inside a variable as well. 00:04:32.490 --> 00:04:33.560 OK so. 00:04:33.890 --> 00:04:42.420 So the query is going to go inside quotes double quotes and we can write our query there. 00:04:42.570 --> 00:04:55.440 So to create a database we get our rights create a database and then we can choose a name for our database 00:04:55.440 --> 00:04:56.030 . 00:04:56.040 --> 00:05:00.740 So for example let's create a store database for example. 00:05:02.280 --> 00:05:04.300 OK. 00:05:04.560 --> 00:05:08.230 That's a simple ask you all query. 00:05:08.400 --> 00:05:12.190 So our query now is stored inside the school variable. 00:05:12.510 --> 00:05:19.120 So now to execute the query we are going to use a function. 00:05:19.380 --> 00:05:22.530 My Escurial II query. 00:05:22.560 --> 00:05:28.910 So this function is going to perform a query on the database. 00:05:30.030 --> 00:05:35.310 Since we are just creating a database it's going to perform the query actually on the server and creates 00:05:35.370 --> 00:05:38.130 a database for us. 00:05:38.400 --> 00:05:44.030 OK so the function is like query is going to return an object. 00:05:44.070 --> 00:05:48.770 If it's successful otherwise it is going to return a boolean false. 00:05:49.020 --> 00:05:49.740 OK. 00:05:50.040 --> 00:05:57.720 So to check if the query has been successfully executed we can just check the condition 00:06:00.520 --> 00:06:03.680 my is Kulai query or the return of the function. 00:06:03.720 --> 00:06:14.300 My is my underscore query and this function is going to take a couple of promises so the first barometer 00:06:14.310 --> 00:06:18.190 is going to be the link variable or the link object. 00:06:18.480 --> 00:06:19.160 OK. 00:06:19.260 --> 00:06:25.700 And the second argument is is going to be the query to execute. 00:06:26.310 --> 00:06:27.200 OK. 00:06:27.690 --> 00:06:34.410 So if it's if it's successful we are going to echo a paragraph 00:06:38.340 --> 00:06:39.660 saying that's 00:06:42.210 --> 00:06:50.820 database store was created successfully. 00:06:51.990 --> 00:06:56.320 OK. 00:06:58.170 --> 00:06:58.800 Else 00:07:04.270 --> 00:07:09.690 we are going to echo. 00:07:10.290 --> 00:07:13.980 So let's have a string 00:07:16.950 --> 00:07:17.660 error. 00:07:17.990 --> 00:07:18.980 OK. 00:07:19.080 --> 00:07:24.670 And then we can say unable to execute the query. 00:07:24.730 --> 00:07:25.110 OK. 00:07:25.110 --> 00:07:26.730 Or you can just write the. 00:07:26.760 --> 00:07:29.680 Copy the variable as here. 00:07:30.140 --> 00:07:31.960 OK. 00:07:32.630 --> 00:07:40.230 And if you want to weaken the error string of the corresponding to the query so we're going to use 00:07:46.500 --> 00:07:47.790 another function. 00:07:47.790 --> 00:07:51.270 It's my Kulai error. 00:07:51.270 --> 00:07:51.900 OK. 00:07:52.080 --> 00:07:58.650 And this one unlike the my Acholi connects Eric is going to return a string description of the last 00:07:58.650 --> 00:08:00.710 error not the last K'NEX error. 00:08:00.780 --> 00:08:04.090 OK so this one is going to return the last error. 00:08:04.150 --> 00:08:04.400 Ok 00:08:07.380 --> 00:08:11.680 so my secure ally underscore error. 00:08:12.180 --> 00:08:13.740 And it's going to take as a barometer 00:08:16.440 --> 00:08:20.670 the variable link. 00:08:20.690 --> 00:08:20.930 OK 00:08:20.950 --> 00:08:29.400 .