Storing the details of users in a SQLite database












-1














I am making an application that allow users to register and their details are stored in a SQLite database. There is nothing wrong with the code down below but I want to know if I can make it more efficient.



I am making this application for my a level computing course work and there is an emphasis on the efficiency of our code which is why I want to know if I can make my code more efficient. The database is called users and I want to store the names usernames and passwords of the users in the database.



if (File.Exists("users.sqlite"))
{
SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");

try
{
con.Open();
string Query = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

SQLiteCommand cmd = new SQLiteCommand(Query, con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
MessageBox.Show("Error registering the user", "Please try again");

}
}
else
{

SQLiteConnection.CreateFile("users.sqlite");

SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");
con.Open();

string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";

SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

sql = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

con.Close();
}









share|improve this question









New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
    – radarbob
    3 hours ago


















-1














I am making an application that allow users to register and their details are stored in a SQLite database. There is nothing wrong with the code down below but I want to know if I can make it more efficient.



I am making this application for my a level computing course work and there is an emphasis on the efficiency of our code which is why I want to know if I can make my code more efficient. The database is called users and I want to store the names usernames and passwords of the users in the database.



if (File.Exists("users.sqlite"))
{
SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");

try
{
con.Open();
string Query = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

SQLiteCommand cmd = new SQLiteCommand(Query, con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
MessageBox.Show("Error registering the user", "Please try again");

}
}
else
{

SQLiteConnection.CreateFile("users.sqlite");

SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");
con.Open();

string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";

SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

sql = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

con.Close();
}









share|improve this question









New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
    – radarbob
    3 hours ago
















-1












-1








-1







I am making an application that allow users to register and their details are stored in a SQLite database. There is nothing wrong with the code down below but I want to know if I can make it more efficient.



I am making this application for my a level computing course work and there is an emphasis on the efficiency of our code which is why I want to know if I can make my code more efficient. The database is called users and I want to store the names usernames and passwords of the users in the database.



if (File.Exists("users.sqlite"))
{
SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");

try
{
con.Open();
string Query = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

SQLiteCommand cmd = new SQLiteCommand(Query, con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
MessageBox.Show("Error registering the user", "Please try again");

}
}
else
{

SQLiteConnection.CreateFile("users.sqlite");

SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");
con.Open();

string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";

SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

sql = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

con.Close();
}









share|improve this question









New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am making an application that allow users to register and their details are stored in a SQLite database. There is nothing wrong with the code down below but I want to know if I can make it more efficient.



I am making this application for my a level computing course work and there is an emphasis on the efficiency of our code which is why I want to know if I can make my code more efficient. The database is called users and I want to store the names usernames and passwords of the users in the database.



if (File.Exists("users.sqlite"))
{
SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");

try
{
con.Open();
string Query = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

SQLiteCommand cmd = new SQLiteCommand(Query, con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
MessageBox.Show("Error registering the user", "Please try again");

}
}
else
{

SQLiteConnection.CreateFile("users.sqlite");

SQLiteConnection con = new SQLiteConnection("Data Source=users.sqlite;Version=3;");
con.Open();

string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";

SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

sql = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";

command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();

con.Close();
}






c# database sqlite






share|improve this question









New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 50 secs ago









Jamal

30.3k11116226




30.3k11116226






New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









Christo Polachan

1




1




New contributor




Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Christo Polachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
    – radarbob
    3 hours ago




















  • If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
    – radarbob
    3 hours ago


















If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
– radarbob
3 hours ago






If you mean execution performance then the database is typically where there is more potential for efficiency gains. That is highly dependent on the database's schema and how the DB is used/queried. Regarding the C# code the MSDN documentation may or may not have some minutia on how to optimally use data adapters. And even then I'd say the DB matters much more. Any C# code changes I can suggest will not help performance as a practical matter.
– radarbob
3 hours ago












1 Answer
1






active

oldest

votes


















0















  • Create objects before opening the connection.

  • Limit the number of connection open/close cycles.

  • Handle "outliers" up front to eliminate exception handling structures. I.E. creating the database file.

  • Descriptive variable names.

  • Invert boolean logic to eliminate unnecessary control structure.

  • TODO


    • Catch specific exceptions so you can know what crashed. i.e. creating the table vs inserting data.

    • Put exception object details in the error message to help know what happened

    • Research the using keyword, how it's used to with data adapter connection objects. Facilitates automatic destruction of objects after use.




.



 SQLiteConnection users = new SQLiteConnection("DataSource=users.sqlite;Version=3;");
string insert = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";
SQLiteCommand insertCmd = new SQLiteCommand(Query, con);
SQLiteCommand createDbCmd = CreateDB();

try
{
users.Open();
if(createDbCmd != null) createDbCmd.ExecuteNonQuery();
insertCmd.ExecuteNonQuery();
users.Close();
}
catch (Exception)
{
MessageBox.Show("Error registering the user", "Please try again");
}



// returns null if file exists
public SQLiteCommand CreateDB()
{
if (File.Exists("users.sqlite")) return null;

SQLiteConnection.CreateFile("users.sqlite");
string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";
SQLiteCommand cmd = new SQLiteCommand(sql, con);
return new SQLiteCommand(sql, con);
}





share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Christo Polachan is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210902%2fstoring-the-details-of-users-in-a-sqlite-database%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    • Create objects before opening the connection.

    • Limit the number of connection open/close cycles.

    • Handle "outliers" up front to eliminate exception handling structures. I.E. creating the database file.

    • Descriptive variable names.

    • Invert boolean logic to eliminate unnecessary control structure.

    • TODO


      • Catch specific exceptions so you can know what crashed. i.e. creating the table vs inserting data.

      • Put exception object details in the error message to help know what happened

      • Research the using keyword, how it's used to with data adapter connection objects. Facilitates automatic destruction of objects after use.




    .



     SQLiteConnection users = new SQLiteConnection("DataSource=users.sqlite;Version=3;");
    string insert = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";
    SQLiteCommand insertCmd = new SQLiteCommand(Query, con);
    SQLiteCommand createDbCmd = CreateDB();

    try
    {
    users.Open();
    if(createDbCmd != null) createDbCmd.ExecuteNonQuery();
    insertCmd.ExecuteNonQuery();
    users.Close();
    }
    catch (Exception)
    {
    MessageBox.Show("Error registering the user", "Please try again");
    }



    // returns null if file exists
    public SQLiteCommand CreateDB()
    {
    if (File.Exists("users.sqlite")) return null;

    SQLiteConnection.CreateFile("users.sqlite");
    string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";
    SQLiteCommand cmd = new SQLiteCommand(sql, con);
    return new SQLiteCommand(sql, con);
    }





    share|improve this answer




























      0















      • Create objects before opening the connection.

      • Limit the number of connection open/close cycles.

      • Handle "outliers" up front to eliminate exception handling structures. I.E. creating the database file.

      • Descriptive variable names.

      • Invert boolean logic to eliminate unnecessary control structure.

      • TODO


        • Catch specific exceptions so you can know what crashed. i.e. creating the table vs inserting data.

        • Put exception object details in the error message to help know what happened

        • Research the using keyword, how it's used to with data adapter connection objects. Facilitates automatic destruction of objects after use.




      .



       SQLiteConnection users = new SQLiteConnection("DataSource=users.sqlite;Version=3;");
      string insert = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";
      SQLiteCommand insertCmd = new SQLiteCommand(Query, con);
      SQLiteCommand createDbCmd = CreateDB();

      try
      {
      users.Open();
      if(createDbCmd != null) createDbCmd.ExecuteNonQuery();
      insertCmd.ExecuteNonQuery();
      users.Close();
      }
      catch (Exception)
      {
      MessageBox.Show("Error registering the user", "Please try again");
      }



      // returns null if file exists
      public SQLiteCommand CreateDB()
      {
      if (File.Exists("users.sqlite")) return null;

      SQLiteConnection.CreateFile("users.sqlite");
      string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";
      SQLiteCommand cmd = new SQLiteCommand(sql, con);
      return new SQLiteCommand(sql, con);
      }





      share|improve this answer


























        0












        0








        0







        • Create objects before opening the connection.

        • Limit the number of connection open/close cycles.

        • Handle "outliers" up front to eliminate exception handling structures. I.E. creating the database file.

        • Descriptive variable names.

        • Invert boolean logic to eliminate unnecessary control structure.

        • TODO


          • Catch specific exceptions so you can know what crashed. i.e. creating the table vs inserting data.

          • Put exception object details in the error message to help know what happened

          • Research the using keyword, how it's used to with data adapter connection objects. Facilitates automatic destruction of objects after use.




        .



         SQLiteConnection users = new SQLiteConnection("DataSource=users.sqlite;Version=3;");
        string insert = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";
        SQLiteCommand insertCmd = new SQLiteCommand(Query, con);
        SQLiteCommand createDbCmd = CreateDB();

        try
        {
        users.Open();
        if(createDbCmd != null) createDbCmd.ExecuteNonQuery();
        insertCmd.ExecuteNonQuery();
        users.Close();
        }
        catch (Exception)
        {
        MessageBox.Show("Error registering the user", "Please try again");
        }



        // returns null if file exists
        public SQLiteCommand CreateDB()
        {
        if (File.Exists("users.sqlite")) return null;

        SQLiteConnection.CreateFile("users.sqlite");
        string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        return new SQLiteCommand(sql, con);
        }





        share|improve this answer















        • Create objects before opening the connection.

        • Limit the number of connection open/close cycles.

        • Handle "outliers" up front to eliminate exception handling structures. I.E. creating the database file.

        • Descriptive variable names.

        • Invert boolean logic to eliminate unnecessary control structure.

        • TODO


          • Catch specific exceptions so you can know what crashed. i.e. creating the table vs inserting data.

          • Put exception object details in the error message to help know what happened

          • Research the using keyword, how it's used to with data adapter connection objects. Facilitates automatic destruction of objects after use.




        .



         SQLiteConnection users = new SQLiteConnection("DataSource=users.sqlite;Version=3;");
        string insert = "insert into user_info (name, username, password) values ('" + fullname + "', '" + username + "','" + password + "')";
        SQLiteCommand insertCmd = new SQLiteCommand(Query, con);
        SQLiteCommand createDbCmd = CreateDB();

        try
        {
        users.Open();
        if(createDbCmd != null) createDbCmd.ExecuteNonQuery();
        insertCmd.ExecuteNonQuery();
        users.Close();
        }
        catch (Exception)
        {
        MessageBox.Show("Error registering the user", "Please try again");
        }



        // returns null if file exists
        public SQLiteCommand CreateDB()
        {
        if (File.Exists("users.sqlite")) return null;

        SQLiteConnection.CreateFile("users.sqlite");
        string sql = "create table user_info (name varchar(20),username varchar(20),password varchar(20))";
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        return new SQLiteCommand(sql, con);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 hours ago

























        answered 2 hours ago









        radarbob

        5,3541026




        5,3541026






















            Christo Polachan is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Christo Polachan is a new contributor. Be nice, and check out our Code of Conduct.













            Christo Polachan is a new contributor. Be nice, and check out our Code of Conduct.












            Christo Polachan is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210902%2fstoring-the-details-of-users-in-a-sqlite-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'