Recent

Author Topic: Problems with Updating database through Lazarus  (Read 12304 times)

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Problems with Updating database through Lazarus
« Reply #15 on: February 12, 2014, 02:07:42 am »
I tried to guess, and when I made the database read-only I receive a similar error message. Check the attachment.

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: Problems with Updating database through Lazarus
« Reply #16 on: February 12, 2014, 09:40:34 am »
- Look if you database file is set to readonly? 
- The drive your working is only readable?

If the debugger is not working, then you can use writeln to show your query and other stuff.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

JZS

  • Full Member
  • ***
  • Posts: 184
Re: Problems with Updating database through Lazarus
« Reply #17 on: February 12, 2014, 12:50:42 pm »
Really impressive work. Even you could improve it a bit more.

As mangakissa has pointed out:
>   'NoOfUsers' has a wrong number.

It's critical to work with variables to hold the current number of users as constant, better query the DB for it.
You used constant at the program starting point to specify how many users you have. Then you updated the number of users at the program life BUT that number has reverted to "3" once it was restarted, regardless of the previously added users, and hence you got Primary Key uniqueness violated.
You need to update the number of users every time you add user even after restart:

// So eliminate this line:
NoOfUsersx := 3;  // At program Main block

What you can do:
// at ConsoleDatabases you can add function that returns the RecordCount of UserDetails Table
Code: [Select]
function db_getRecCount():Integer; begin
  if db_Loaded = true then
  begin
    db_getRecCount:=Source.DataSet.RecordCount;
  end;
end;

Alternatively you can make use of the "QueryDatabase" functions you have already:
Code: [Select]
// Ot main program "AccountsProject":
function HowManyUsers:integer;
var
   Str:String;
begin
  //enclose in Try-Except block to handle unsuccessful queries
  //and validate the returned result
  db_QueryDatabase('SELECT Count(UsernameDB) AS UsersCount FROM UserDetails');
  Str:=db_GetField('UsersCount');
  if str <> '' then
    HowManyUsers := StrToInt(str)
  else
    HowManyUsers := 0;
end;

So "AddNewAccount" function becomes:
Code: [Select]
Procedure AddUserAccounts;
begin
  clrscr;
  writeln('Adding a new user account');
  writeln('');
  NoOfUsers := HowManyUsers + 1;
  writeln('Please enter the username of the new account you would like to create:');
  readln(NewUsersUsername);
  writeln(''); 
...
  // if you might have more than ten users make "UserAccountsArray" dynamic array instead
  UserAccountsArray[NoOfUsers].UserNo := NoOfUsers;
  UserAccountsArray[NoOfUsers].Username := NewUsersUsername;
  UserAccountsArray[NoOfUsers].Password := NewUsersPassword;
  UserAccountsArray[NoOfUsers].AccessLevel := NewUsersAccessLevel;
  UserAccountsArray[NoOfUsers].UnreadMessages := 0;

  writeln('The user account has been successfully created.');
  readln;
end;

And at the program main block:
Code: [Select]
begin
  db_Load('n:/AccountsDatabase.db'); //n:/AccountsDatabase.db//   //C:/Users/Jack/Documents/Lazarus/test.db//
  textcolor(15);
  NoOfBooks := 0;
  MessageNo := 0;
  CloseProgram := false;

  NoOfUsers := HowManyUsers;     // NoOfUsersx := 3;
// This is just an example but you better query the DB for all
//  users (select * from UserDetails) and use "while not EOF do" and load the Array
  For x := 1 to NoOfUsers do   
    begin
      db_QueryDatabase('SELECT * FROM UserDetails where UserNoDB = '+IntToStr(x));

      UserAccountsArray[x].UserNo := StrToInt(db_GetField('UserNoDB'));
      UserAccountsArray[x].Username := db_GetField('UserNameDB');
      UserAccountsArray[x].Password := db_GetField('PasswordDB');
      UserAccountsArray[x].AccessLevel := StrToInt(db_GetField('AccesslevelDB'));
      UserAccountsArray[x].UnreadMessages := StrToInt(db_GetField('UnreadmessagesDB'));
    end;

  Repeat
    Login
  until(CloseProgram=true);
end.


That's all I had to do to get "Add User Account" Working. As well as fixing the "Modify Users Account" listing issue.

PS: You need to validate the query results to handle unexpected errors, for instance, empty string when converted to integer will give error.
« Last Edit: February 12, 2014, 12:54:02 pm by JZS »
I use recent stable release

 

TinyPortal © 2005-2018