SQLite Inserts - How to Insert Any Text while Avoiding Syntax Errors using Parameterized Values - sql

I'm curious to know a proper way to insert text strings into a database regardless of what characters are contained within the string. What I'm getting at is if I have a string for example that contains single quotes or any other character that is reserved as a 'special' SQL character.
The issue specifically that I'm dealing with is that I don't have control over the possible 'Text' that is being inserted into my database as they text is generated by my application.
One example of where my application fails to insert properly is when there's an error message that happens to contain single quotes. Single quotes are used by SQL statements insert text of course and so I can't insert text that also contains single quotes (outside of using ascii value char(##) function). The issue I'm dealing with is that I'm setting the output of my application to a string variable to then be inserted into my database so I can log activity whether that be standard output or catching errors.
How do I simply INSERT what's contained in my string variable while avoiding all of the SQL Special characters?
Do I need to manually account for all of the SQL Special characters and replace them in my string prior to insert? This sounds like a hit or miss and I'm hoping that there's something already built to accommodate this situation.
Sample Pseudo Code to get the point across:
Let's say an error occurred within the app and it needs to be logged. The error string could be:
Error String: "Error in function calculateStats() parameter 'pBoolStatCheck' is Null"
Now I assign to my string variable within my app and build up the SQL Insert string.
var insertString = "Error in function calculateStats() parameter 'pBoolStatCheck' is Null"
INSERT INTO outputLog(outputLogText)
VALUES ('insertString'); --This will Fail
--Fails due to the variable 'insertString' containing single quotes.
INSERT INTO outputLog(outputLogText)
VALUES ('Error in function calculateStats() parameter 'pBoolStatCheck' is Null');
In closing - since I have no control over the text that could be created by my application how do I account for all of the possible characters that could break my insert?
The code my current application encountering this issue is written in Visual Basic. The database I'm working with is SQLite.
The final solution based on answers received by this post:
Public Sub saveOutputLog(pTextOutput, pTextColor, pNumNewLines, plogType, pMethodSub)
Dim strTableName As String = "outputLog"
Dim sqlInsert As String = "INSERT INTO " & strTableName & "(" &
"outputLog, logColor, numNewLines, logType, method" &
") VALUES (#outputLog, #logColor, #numNewLines, #logType, #method);"
Try
'Open the Database
outputLogDBConn.Open()
'Create/Use a command object via the connection object to insert data into the outputLog table
Using cmd = outputLogDBConn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = sqlInsert
cmd.Parameters.AddWithValue("#outputLog", pTextOutput)
cmd.Parameters.AddWithValue("#logColor", pTextColor)
cmd.Parameters.AddWithValue("#numNewLines", pNumNewLines)
cmd.Parameters.AddWithValue("#logType", plogType)
cmd.Parameters.AddWithValue("#method", pMethodSub)
'Execute the command using above Insert and added Parameters.
cmd.ExecuteNonQuery()
End Using 'Using outputLogDBComm
outputLogDBConn.Close()
Catch ex As Exception
outputLogDBConn.Close()
End Try
End Sub

This problem is very similar to the problem of preventing SQL Injection vulnerabilities in your code. Queries are very easy to break, and while yours breaks in a way that is harmless and annoying, these can be taken to levels where certain inputs can completely destroy your database!
One of the best ways to approach this is by using parameterized queries. This approach is pretty simple; you write the query first with 'placeholders' for the parameters you will send. Once you are ready in your program you can then 'bind' those parameters to the placeholders.
It would look something like the following:
Dim command = New SqlCommand("INSERT INTO outputLog(outputLogText) VALUES (#stringToInsert)", connection);
.
.
.
code
.
.
.
command.Parameters.AddWithValue("#stringToInsert", errorMessage)
.
.
.
execute query
In the above you see that #stringToInsert is the placeholder which is only bound at a later time. It doesn't matter what the variable errorMessage contains, since it will not cause the query to function in a way where the input causes it to potentially break.
There are a lot of resources on this:
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.prepare?view=netframework-4.7.2

Database objects like connections not only need to be closed but also disposed. A Using block will ensure this will happen even if there is an error.
The .Add is better than .AddWithValue for a number of reasons. See https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ and http://www.dbdelta.com/addwithvalue-is-evil/
The parameters of .Add are the name of the parameter, the datatype in the database and optionally the size of the field. You will need to check the database for the last 2.
Open the connection at the last minute befor the execute.
If you are using OleDb with Access, you need to make sure the parameters are added in the same order as they appear in the sql statement.
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("INSERT INTO outputLog(outputLogText) VALUES (#outputLogText);", cn)
cmd.Parameters.Add("#outputLogText", SqlDbType.VarChar, 100).Value = TextBox1.Text
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using

Related

Errors appending to a table in Access VBA

I have an Access database with a VBA module that assembles a JSON string in a variable: JsonStr2 that I want to place in a simple table: JSONforAPI with two fields:
ID = Auto number and
JSON = Long String.
The JSON is valid and Jsonstr2 prints in the immediate window on Debug.Print.
When I run the code line:
DoCmd.RunSQL "INSERT INTO JSONforAPI (JSON) VALUES (" & Jsonstr2 & ")"
I get:
Run time error ‘3075’ Malformed GUID in query expression ‘{"order":{"CustomerID":"19"’.”
On clicking Help I get:
This command isn’t available. Your organization’s administrator turned off the service required to use this feature.
I get the same even if I create a Query Def.
I would be very grateful if someone could tell me what is wrong.
Since you are concatenating your value with the remainder of your SQL statement, none of the delimiters or other special characters within the string held by Jsonstr2 will be escaped and therefore you'll end up with all kinds of malformed strings within the SQL statement.
You should instead use parameters to avoid the need to escape the string, e.g.:
With CurrentDb.CreateQueryDef("","insert into jsonforapi (json) values (#json)")
.Parameters("#json") = jsonstr2
.Execute
End With
You could try this:
DoCmd.RunSQL "INSERT INTO JSONforAPI (JSON) VALUES ('" & Jsonstr2 & "')"
This will single quote Jsonstr2.
As you did not quote the string, it is passed as GUID, not a simple string.

VB.NET - Getting value from MySQL database and inserting it into Ms. Access (MDB) database

I want to Export database from MySQL and import it to Ms. Access (MDB) database. I've tried to code a long LoC to process importing database into a DataTable variable and store the value into it.
This is my code for getting value from adm_users database on MySQL
a_query = "SELECT * FROM adm_users where uac_code='4' or uac_code='4A' or uac_code='1'"
a_da = New MySqlDataAdapter(a_query, db_conn)
a_da.Fill(a_ds, "adm_users")
a_dt = a_ds.Tables("adm_users")
and then i want to store a_dt value into MDB database with this code
For x = 0 To a_dt.Rows.Count - 1
b_query = "INSERT INTO user_login
(userid, username, password, uac) VALUES
('" + a_dt.Rows(x).Item("userid") + "', '" + a_dt.Rows(x).Item("username") + "', '" + a_dt.Rows(x).Item("userpwd") + "', '2')"
b_sql = New OleDbCommand(b_query, ms_conn)
b_sql.ExecuteReader()
Next
and then trying to find a string queries stored in b_query, it looks like this
INSERT INTO user_login
(userid, username, password, uac) VALUES
('admin', 'Varenicou', 'administrator', '2')
and then the return value is: "syntax error in INSERT INTO statement"
i found there's nothing wrong with the queries, so i replace b_query variable with simple query like "SELECT FROM user_login" and the return value is: "syntax error in SELECT * FROM statement"
maybe i'm too dumb to ask this question, but my opinion is that VB.NET thinks that i've already opened a connection to MySQL Database and must always on MySQL database. So, when i open database connection with Ms. Access it'll return value like that and VB.NET confused with "which database you wanna use?"
any idea for me to do this task? i've asked my friend for some suggestions, he said that i need to work with class, i also have another idea to work with another new project that doing this task for inputing into Ms. Access database by exporting MySQL database into CSV first.
EDIT:
Having reread your question, I can tell you that the specific issue you're having is due to the fact that "password" is a reserved word in Jet/Access SQL. Any time you want to use a reserved word as an identifier in SQL, you need to escape it. For Access, you do that by wrapping it in brackets, e.g.
INSERT INTO user_login (userid, username, [password], uac)
VALUES (#userid, #username, #password, #uac)
My original advice below still stands though.
ORIGINAL:
If you have a DataTable then you don't loop through it to save the data. Just as you make a single Fill call on a data adapter to retrieve data, so you make a single Update call to save data. How does it make sense to call ExecuteReader when there's nothing to read? At least call ExecuteNonQuery but even that is not ideal.
If you want to insert the data it contains then you have to configure the InsertCommand of the second data adapter. You also need all the DataRows in the DataTable to have a RowState of Added. To do that, you simply set AcceptChangesDuringFill to False on the first data adapter. When you call Fill, it adds all the rows and thus their RowState is Added. It then calls AcceptChanges, which sets all the RowStates to Unchanged. Tell it not to call AcceptChanges and you're ready to insert.
Here's an example of getting data from one database and inserting into another using two data adapters:
Dim table As New DataTable
Using sourceAdapter As New MySqlDataAdapter("SELECT Column1, Column2 FROM MyTable",
"source connection string here") With {.AcceptChangesDuringFill = False}
sourceAdapter.Fill(table)
End Using
Using destinationConnection As New OleDbConnection("destination connection string here"),
destinationCommand As New OleDbCommand("INSERT INTO MyTable (Column1, Column2) VALUES (#Column1, #Column2)",
destinationConnection),
destinationAdapter As New OleDbDataAdapter With {.InsertCommand = destinationCommand}
With destinationCommand.Parameters
.Add("#Column1", OleDbType.VarChar, 50, "Column1")
.Add("#Column2", OleDbType.Integer, 0, "Column2")
End With
destinationAdapter.Update(table)
End Using
As you can see, it's fairly simple stuff. One data adapter with a SelectCommand and another with an InsertCommand. There's nothing special about the adding of the parameters, but you do need to know about parameters in the first place. If you don't know how to use parameters with ADO.NET then you should learn immediately. You can find my own explanation here.

Read Value from Database in TextBox when Combobox text changes VB.NET

I have a list of Users Names in ComboBox and Some TextBoxes. When ComboBox text changes (i.e I select some username from ComboBox), The TextBoxes are filled with user details from the database.
I have code to achieve this in SQL Database. But these queries are not working with MsAccess database.
MysqlConn = New MySqlConnection
Mysql.ConnectionString = "server=localhost;user=root;password=root;database=database"
Dim READER As MySqlDataReader
Try
MysqlConn.open()
Dim Query As String
Query("select * from database.usernames where name='" & ComboBox1.Text & "'")
Command = New MySqlCommand(Query, MysqlConn)
READER = Command.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetString("name")
End While
End Try
Here is my answer. Please don't get overwhelmed by it. ;)
Broken code
First of all, as I see it, the code you provided cannot work at all, because:
your Query variable is initialized in an invalid (or at least a very exotic) way. You probably want to use something like:
Dim Query As String
Query = "select * from database.usernames where name='" & ComboBox1.Text & "'"
or in a single line:
Dim Query As String = "select * from database.usernames where name='" & ComboBox1.Text & "'"
you try to assign the connection string to the ConnectionString property of a nonexistent Mysql variable. Or the variable exists because it is declared somewhere else, which might be a bug in your code snippet here. But I assume you want to assign the connection string to the MysqlConn.ConnectionString property instead.
you have not declared the MysqlConn and Command variables anywhere. You only just assign to them. (I will simply assume you have declared the variables correctly somewhere else in your code...)
the IDataRecord interface does not provide a GetString(name As String) method overload. So unless you have defined a custom extension method for it, you probably need to use the IDataRecord.GetOrdinal(name As String) method as well, or use the column index instead of the column name.
Anyway, the code you provided uses MySQL. So I assume that MySQL is the "SQL Database" you are using successfully. And that seems to work, as you say? Well... Hmmm... Then I will simply assume your code snippet is completely correct and works perfectly with MySQL... :/
MS Access vs. MySQL
Using MS Access requires other data access classes (probably the ones in namespace System.Data.OleDb) and another connection string. You could take a look at this ADO.NET OleDb example for MS Access in the Microsoft documentation.
You probably even have to update your SQL query, because every database system uses its own SQL dialect. You might want to consult the Office documentation for that. But your query is quite simple, so perhaps all you have to do to make it work with MS Access is:
remove the database name and use only the table name, and
delimit the name identifier (since it is a reserved keyword in MS Access).
I personally delimit all identifiers in my SQL queries, just to avoid unintended conflicts with reserved keywords. So I would personally use something like this:
select * from [usernames] where [name] = '...'
Additional tips
Also, I would like to provide you some additional (unrelated) tips regarding improving your code:
Use Using-statements with variables of an IDisposable type as much as possible. Those types/classes do not implement that interface if there isn't a good reason for it, so I consider it not unimportant to call Dispose when you are done with such disposable objects (or using a Using statement to call Dispose implicitly).
Use SQL parameters (if possible) to avoid SQL injection vulnerabilities. Look at this StackOverflow question and its answer for an example of how to use SQL parameters with MS Access.
Example
You may take a look at the following code snippet. It might not provide a working example out-of-the-box, but you might get some useful/practical ideas from it:
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data\\database.mdb;User Id=admin;Password="
Dim query As String = "select * from [usernames] where [name] = #Name"
Using conn As New OleDbConnection(connectionString)
Using command As New OleDbCommand(query)
command.Parameters.Add("#Name", OleDbType.VarChar, 50).Value = ComboBox1.Text
conn.Open()
Using reader As OleDbDataReader = command.ExecuteReader
If reader.Read Then
textbox1.Text = reader.GetString(reader.GetOrdinal("name"))
End If
End Using
End Using
End Using

Using the Replace() function in a parameterized query

I have the below mentioned code trying to perform an update/replace function on each row in the database where it finds the "Search" Pattern being stored in the parameter. However, when I run the query in the VB.NET program, the program is skipping over the row that it is supposed to be hitting.
For example, say the intQuestionID variable is set to 4. And within the QuestionStacks table is a value ;4;0;14;24;34;. The SQL should be replacing the first ;4; with ;0; but it is not. In fact, nothing is being changed. Even if the value is a sole ;4;.
Dim sqlClearQuestion As New OleDb.OleDbCommand("UPDATE QuestionStacks SET QuestionData = REPLACE([QuestionData], "";#0;"", "";0;"") ", DBConnection)
sqlClearQuestion.Parameters.AddWithValue("#0", intQuestionID)
sqlClearQuestion.ExecuteNonQuery()
I've checked this query by running the SQL in the Query Builder in Access 2016 and it works fine as intended. However, attempting to use it in this VB.NET function, it is not yielding the desired result. What change is needed for this?
N.B. I know that .AddWithValue on Access SQL isn't needed, and is actually changed to something else (Read: https://stackoverflow.com/a/6794245/692250). But with only one parameter, there shouldn't be any problem with this.
One should always be suspicious when they see delimiters or any other decoration surrounding a parameter placeholder. Parameter placeholders should appear alone, with any required special characters in the parameter value. For example, a very common mistake is to try and use
' no good
cmd.CommandText = "SELECT * FROM Students WHERE FirstName LIKE '?%'"
cmd.Parameters.AddWithValue("?", theName)
when the correct approach is to use
' good
cmd.CommandText = "SELECT * FROM Students WHERE FirstName LIKE ?"
cmd.Parameters.AddWithValue("?", theName & "%")
In your particular case, you'll want to use
cmd.CommandText = "UPDATE QuestionStacks SET QuestionData = REPLACE([QuestionData], ?, "";0;"") "
cmd.Parameters.AddWithValue("?", ";" & intQuestionID & ";")

VB Access DB Update statement

I am new to this forum, please could you help me get this code to work, when i execute it, it simply does nothing and does not update the DB. If i remove the square brackets it gives an error: "SYNTAX ERROR in UPDATE statement"
Any help appreciated!
Dim connection As OleDbConnection
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UserDB.accdb;Jet OLEDB:Database;")
connection.Open()
Dim pass As String
pass = txtconfirm.Text
Dim user As String
user = LoginForm.UsernameTextBox.Text
Dim query As String = "UPDATE [Users] SET [Password]= '" & pass & "' WHERE [Username]= '" & user & "';"
Dim command As New OleDbCommand(query, connection)
command.ExecuteNonQuery()
connection.Close()
Given your actual connection string, the database that will be updated is the one in the directory where your application starts. This means that if you work with a WinForms application this folder is \BIN\DEBUG or x86 variant. If there is not error then you could get the return value of the ExecuteNonQuery call to verify if a record has been updated or not
Dim rowsUpdated = command.ExecuteNonQuery()
MessageBox.Show("Record updated count = " & rowsUpdated)
If this value is not zero then your database has been updated and you are looking for changes in the wrong database. Check the one in the BIN\DEBUG folder.
In any case your code has big problems. If your variables user or pass contain a single quote, then your code will crash again because your string concatenation will form an invalid SQL. As usual the only workaround is to use a parameterized query
Dim pass = txtconfirm.Text
Dim user = LoginForm.UsernameTextBox.Text
Dim query As String = "UPDATE [Users] SET [Password]= #p1 WHERE [Username]= #p2"
Using connection = New OleDbConnection("...........")
Using command As New OleDbCommand(query, connection)
connection.Open()
command.Parameters.Add("#p1", OleDbType.VarWChar).Value = pass
command.Parameters.Add("#p2", OleDbType.VarWChar).Value = user
command.ExecuteNonQuery()
End Using
End Using
The parameterized approach has many advantages. Your query text is more readable, there is no misunderstanding between your code and the values expected by your database engine. And while not easy to exploit with MS-Access there is no problem with Sql Injection
I think Steve presents a much better approach for you coding this...
Let me just throw out a few more things:
The reason you can't take those brackets out is some of your column names are reserved words; just FYI.
Since you report "it does nothing..." when you execute, it sounds like you have a valid connection and sql syntax, in which case my next step would be to copy the sql command text while in debug mode, change it to a select and run it in your DB. You should get one result when you do. If not, either your criteria or field contents are not what you think they are...
Just change the Update table SET field-value ... to SELECT * FROM table and leave the WHERE clause as is.