Fix db3 related calls.

This commit is contained in:
zongor 2023-09-10 11:45:42 -04:00
parent d81fdb055a
commit 7e2859ffc0
3 changed files with 84 additions and 47 deletions

Binary file not shown.

View File

@ -1,5 +1,6 @@
program main
use www
use db
implicit none
character, dimension(:), allocatable :: form_data
@ -21,7 +22,7 @@ program main
call request(form_data, length)
if (length .gt. 0) then
inquire (file=trim(adjustl(db_path)), exist=exist)
inquire (file=db_path(:Len_Trim(db_path)), exist=exist)
if (.not. exist) then
call create_db(db_path)
end if
@ -39,17 +40,24 @@ contains
subroutine create_db(db_path)
character(len=128), intent(in) :: db_path
character(len=:), allocatable :: command
command = 'sqlite3 '//trim(adjustl(db_path)) &
//' ''CREATE TABLE users ' &
//'(id INTEGER PRIMARY KEY ASC, ' &
//'username TEXT, password TEXT, ' &
//'apperance TEXT, x_pos INTEGER, ' &
//'y_pos INTEGER, last_login INTEGER, ' &
//' created INTEGER);'''
write (12, *) command
call execute_command_line(command)
! character(len=:), allocatable :: command
type(db_type) :: db
integer :: rc
rc = db_open(db, db_path(:Len_Trim(db_path)))
rc = db_create_users(db)
rc = db_close(db)
! command = 'sqlite3 '//db_path &
! //' ''CREATE TABLE users ' &
! //'(id INTEGER PRIMARY KEY ASC, ' &
! //'username TEXT, password TEXT, ' &
! //'apperance TEXT, x_pos INTEGER, ' &
! //'y_pos INTEGER, last_login INTEGER, ' &
! //' created INTEGER);'''
! write (12, *) command
! call execute_command_line(command)
end subroutine create_db
@ -65,7 +73,10 @@ contains
character(len=8) :: appearance
logical :: start = .false.
character(len=:), allocatable :: command
type(db_type) :: db
integer :: rc
! character(len=:), allocatable :: command
j = 1
get_username: do i = 1, length
@ -114,19 +125,22 @@ contains
end do get_appearance
appearance = transfer(request(s_idx:length), appearance)
write (created, *) time()
! write (created, *) time()
! command = 'sqlite3 '//db_path(:Len_Trim(db_path)) &
! //' "INSERT INTO users (username,password' &
! //',apperance,x_pos,y_pos,last_login,created) VALUES (''' &
! //username(:username_len) &
! //''',''' &
! //password(:password_len) &
! //''',''' &
! //appearance(:Len_Trim(appearance)) &
! //''',0,0,0,' &
! //created(:Len_Trim(created))//');"'
! call execute_command_line(command)
command = 'sqlite3 '//db_path(:Len_Trim(db_path)) &
//' "INSERT INTO users (username,password' &
//',apperance,x_pos,y_pos,last_login,created) VALUES (''' &
// username(:username_len) &
//''',''' &
// password(:password_len) &
//''',''' &
// appearance(:Len_Trim(appearance)) &
//''',0,0,0,' &
//created(:Len_Trim(created))//');"'
call execute_command_line(command)
rc = db_open(db, db_path(:Len_Trim(db_path)))
rc = db_add_user(db, username(:username_len), password(:password_len), appearance(:Len_Trim(appearance)), 0, 0, 0, time())
rc = db_close(db)
end subroutine add_user

View File

@ -65,12 +65,12 @@ contains
if (rc /= SQLITE_OK) return
end function db_create_users
integer function db_add_user(db, name) result(rc)
integer function db_add_user(db, username, password, apperance, x_pos, y_pos, last_login, created) result(rc)
!! Adds student to database.
type(db_type), intent(inout) :: db
character(len=:), intent(in) :: username
character(len=:), intent(in) :: password
character(len=:), intent(in) :: apperance
character(len=*), intent(in) :: username
character(len=*), intent(in) :: password
character(len=*), intent(in) :: apperance
integer, intent(in) :: x_pos
integer, intent(in) :: y_pos
integer, intent(in) :: last_login
@ -78,7 +78,7 @@ contains
type(c_ptr) :: stmt
! Insert values through prepared statement.
rc = sqlite3_prepare_v2(db%ptr, "INSERT INTO users(username, password, "
rc = sqlite3_prepare_v2(db%ptr, "INSERT INTO users(username, password, " &
//"apperance, x_pos, y_pos, last_login, created) VALUES (?,?,?,?,?,?,?)", stmt)
call db_error(rc, 'sqlite3_prepare_v2()')
@ -88,14 +88,14 @@ contains
call db_error(rc, 'sqlite3_bind_text()')
rc = sqlite3_bind_text(stmt, 3, apperance)
call db_error(rc, 'sqlite3_bind_text()')
rc = sqlite3_bind_integer(stmt, 4, x_pos)
call db_error(rc, 'sqlite3_bind_integer()')
rc = sqlite3_bind_integer(stmt, 5, y_pos)
call db_error(rc, 'sqlite3_bind_integer()')
rc = sqlite3_bind_integer(stmt, 6, last_login)
call db_error(rc, 'sqlite3_bind_integer()')
rc = sqlite3_bind_integer(stmt, 7, created)
call db_error(rc, 'sqlite3_bind_integer()')
rc = sqlite3_bind_int(stmt, 4, x_pos)
call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_int(stmt, 5, y_pos)
call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_int(stmt, 6, last_login)
call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_int(stmt, 7, created)
call db_error(rc, 'sqlite3_bind_int()')
! Insert bound value into database.
rc = sqlite3_step(stmt)
@ -104,7 +104,30 @@ contains
! Clean-up prepared statement.
rc = sqlite3_finalize(stmt)
call db_error(rc, 'sqlite3_finalize()')
end function db_add_student
end function db_add_user
integer function db_delete_user(db, username) result(rc)
!! Adds student to database.
type(db_type), intent(inout) :: db
character(len=*), intent(in) :: username
type(c_ptr) :: stmt
! Insert values through prepared statement.
rc = sqlite3_prepare_v2(db%ptr, "DELETE FROM users WHERE users.username = ?;", stmt)
call db_error(rc, 'sqlite3_prepare_v2()')
rc = sqlite3_bind_text(stmt, 1, username)
call db_error(rc, 'sqlite3_bind_text()')
! Insert bound value into database.
rc = sqlite3_step(stmt)
call db_error(rc, 'sqlite3_step()')
! Clean-up prepared statement.
rc = sqlite3_finalize(stmt)
call db_error(rc, 'sqlite3_finalize()')
end function db_delete_user
integer function db_get_users(db) result(rc)
!! Prints number of courses per student to standard output.
@ -115,7 +138,7 @@ contains
character(len=24) :: password
rc = sqlite3_prepare_v2(db%ptr, &
"SELECT username " // &
"SELECT username, password " // &
"FROM users;", stmt)
call db_error(rc, 'sqlite3_prepare_v2()')
@ -126,7 +149,7 @@ contains
case (SQLITE_ROW)
username = sqlite3_column_text(stmt, 0)
password = sqlite3_column_text(stmt, 1)
print '(a),(a)', username, password
write(12, '(a)') username, password
case (SQLITE_DONE)
exit step_loop
@ -150,15 +173,15 @@ contains
if (code == SQLITE_OK .or. code == SQLITE_DONE) return
if (present(proc) .and. present(err_msg)) then
print '(a, ": ", a, " (", i0, ")")', proc, err_msg, code
write(12, '(a, ": ", a, " (", i0, ")")') proc, err_msg, code
return
end if
if (present(proc)) then
print '(a, ": ", i0)', proc, code
write(12, '(a, ": ", i0)') proc, code
return
end if
print '("unknown error: ", i0)', code
write(12, '("unknown error: ", i0)') code
end subroutine db_error
end module db