@@ -12,58 +12,46 @@ const selectAllQuery = "SELECT * FROM movies";
12
12
const deleteTableQuery = "DROP TABLE books" ;
13
13
14
14
const titles = [ "I Am a Cat" , "I Am a Cat" , "SANSHIRO" ] ;
15
- let insertCount = 0 ;
16
15
17
- function main ( ) {
18
- createTable ( ) ;
19
- }
20
-
21
- function createTable ( ) {
22
- db . run ( createTableQuery , insertTitles ) ;
23
- }
24
-
25
- function insertTitles ( ) {
16
+ db . run ( createTableQuery , function ( ) {
26
17
console . log ( "Table created" ) ;
27
- for ( const title of titles ) {
28
- db . run ( insertTitleQuery , [ title ] , displayID ) ;
29
- }
30
- }
31
-
32
- function displayID ( err ) {
33
- if ( err ) {
34
- console . error ( `Error occurred while inserting record: ${ err . message } ` ) ;
35
- } else {
36
- console . log ( `Record inserted successfully with ID: ${ this . lastID } ` ) ;
37
- }
38
- insertCount ++ ;
39
- if ( insertCount === titles . length ) {
40
- fetchAll ( ) ;
41
- }
42
- }
43
-
44
- function fetchAll ( ) {
45
- db . all ( selectAllQuery , displayAll ) ;
46
- }
47
-
48
- function displayAll ( err , rows ) {
49
- if ( err ) {
50
- console . error ( `Error occurred while fetching records: ${ err . message } ` ) ;
51
- } else {
52
- console . log ( "All records fetched successfully" ) ;
53
- for ( const row of rows ) {
54
- console . log ( `id:${ row . id } , title:${ row . title } ` ) ;
18
+ db . run ( insertTitleQuery , titles [ 0 ] , function ( err ) {
19
+ if ( err ) {
20
+ console . error ( `Error occurred while inserting record: ${ err . message } ` ) ;
21
+ } else {
22
+ console . log ( `Record inserted successfully with ID: ${ this . lastID } ` ) ;
55
23
}
56
- }
57
- deleteTable ( ) ;
58
- }
59
-
60
- function deleteTable ( ) {
61
- db . run ( deleteTableQuery , closeDB ) ;
62
- }
63
-
64
- function closeDB ( ) {
65
- console . log ( "Table deleted" ) ;
66
- db . close ( ) ;
67
- }
68
-
69
- main ( ) ;
24
+ db . run ( insertTitleQuery , titles [ 1 ] , function ( err ) {
25
+ if ( err ) {
26
+ console . error ( `Error occurred while inserting record: ${ err . message } ` ) ;
27
+ } else {
28
+ console . log ( `Record inserted successfully with ID: ${ this . lastID } ` ) ;
29
+ }
30
+ db . run ( insertTitleQuery , titles [ 2 ] , function ( err ) {
31
+ if ( err ) {
32
+ console . error (
33
+ `Error occurred while inserting record: ${ err . message } ` ,
34
+ ) ;
35
+ } else {
36
+ console . log ( `Record inserted successfully with ID: ${ this . lastID } ` ) ;
37
+ }
38
+ db . all ( selectAllQuery , function ( err , rows ) {
39
+ if ( err ) {
40
+ console . error (
41
+ `Error occurred while fetching records: ${ err . message } ` ,
42
+ ) ;
43
+ } else {
44
+ console . log ( "All records fetched successfully" ) ;
45
+ for ( const row of rows ) {
46
+ console . log ( `id:${ row . id } , title:${ row . title } ` ) ;
47
+ }
48
+ }
49
+ db . run ( deleteTableQuery , function ( ) {
50
+ console . log ( "Table deleted" ) ;
51
+ db . close ( ) ;
52
+ } ) ;
53
+ } ) ;
54
+ } ) ;
55
+ } ) ;
56
+ } ) ;
57
+ } ) ;
0 commit comments