Unknown column in field list vb.net - mysql

Connected to mysql database with vb.net
When i want to add number through application to Mysql database no problems.
When i type word/letter in field it always popups Unknown column 'text' in 'field list'
I checked the type in mysql database of the field users it is set to 'Varchar' max 50
To add record i use query
query = "insert into table tableName (users,website,amount) values (" + txtname.Text + ",'" + txtsite.Text + "','" + txtamount.Text + "')"

and you are missing single quotes after
values(
corrected query with quotes (and assumption that amount column could be of numeric type. If it is non-numeric you can keep the query as it is):
query = "insert into table tableName(users,website,amount) values ('" + txtname.Text + "','" + txtsite.Text + "','" + Convert.ToDecimal(txtamount.Text) + "')" // or Convert.ToInt64 whichever it could be

Related

SSIS loading .csv flat files using For Each Loop Container : Execute SQL Task expression to populate the audit table with Row Count

I am loading many .csv files into a SQL Server table using SSIS (Visual Studio 2017).
I am using For Each Loop Container. I have two user variables - FileName (string) and RowCnt (int)
I have developed an audit table (dbo.FEfiles) to track each file loaded along with the number of records in each file.
Audit table has two columns:
1) FileName(varchar(20)
2) RowCount(int)
After the setting up of the Data Flow Task inside the For Each Loop Container, I have an Execute SQL Task that connects to my audit table.
In this Execute SQL Task, in the Expression tab, under the SQLStatementSource, I have the following expression to get the file name alone:
"INSERT INTO dbo.FEfiles (FileName) SELECT '" + #[User::Filename] + "' "
This parses correctly when I evaluate the expression, I get:
INSERT INTO dbo.FEfiles (FileName) SELECT 'filename.CSV'
Now, how do I add the variable for the RowCnt, which has integer data type ?
I tried many options such as the one below:
"INSERT INTO dbo.FEfiles (FileName,RowCount) SELECT '" + #[User::Filename] + "', + (DT_WSTR, 12) #[User::RowCnt] "
When I evaluate the expression, I get the following:
INSERT INTO dbo.FEfiles (FileName,RowCount) SELECT 'filename.CSV', + (DT_WSTR, 12) #[User::RowCnt]
I need to instead get this: INSERT INTO dbo.FEfiles (FileName,RowCount) SELECT 'filename.CSV', 0
Can you kindly help me in getting the right expression ?
I even watched this video, but not able to figure out:
https://www.youtube.com/watch?v=Um7tDy9jZRs
Give this a whirl.
"INSERT INTO dbo.FEFiles (FileName,RowCount) VALUES ('" + #[User::Filename] + "'," + (DT_WSTR, 12) #[User::RowCnt] + ")"
Per users comments had to put RowCount into brackets for column name.
"INSERT INTO dbo.FEFiles (FileName,[RowCount]) VALUES ('" + #[User::Filename] + "'," + (DT_WSTR, 12) #[User::RowCnt] + ")"
String parts
"INSERT INTO dbo.FEFiles (FileName,[RowCount]) VALUES ('"
+
#[User::Filename]
+
"',"
+
(DT_WSTR, 12) #[User::RowCnt]
+
")"

Trying to insert date time into sql with variables in the query

String query = "insert into course_data
values (null, '"
+ CourseName + "','"
+ SCrsDesrpTemp + "','"
+ CrsDes + "','"
+ crsurl + "','"
+ youtube + "','"
+ sqlStrDate + "','"
+ crsduration + "','"
+ CrsImg + "','"
+ category + "',"
+ "'Open2Study',
'0.00',
'English',
'Yes','"
+ CrsImgUni + "','"
+ "GETDATE()" + "')";
That is my attempt above. I am trying to insert the current date and time into a date-time column but I keep getting syntax error for the query. It says GETDATE() is not the correct datatype for the column date-time.
Try this for Sql Server:-
ALTER TABLE course_data ADD CONSTRAINT
DF_MyTable_Inserted DEFAULT GETDATE() FOR crsduration
GO
This assumes your table is named course_data, the column is crsduration, and the name of the contstraint is to be DF_MyTable_Inserteddb in
If db in MySQL NOW() for get current date time
NOW()//Current date time
CURDATE()//Current date

Cannot insert into MySQL via NetBeans

Can someone help me with the problem that I'm having now?
I have a set of question(survey). When the user done the survey, it will insert to my database.
However, right now, when I submit the survey, it doesn't insert the result into my database.
here is my code:
try {
//Process - Query from SQL
String strSqlnsert = "INSERT INTO Customers (MembershipID, Contact, Email, Address, SurveyStatus, Subscription) VALUES"
+ " ('"+ member_ID + "', " + contact_num + ", '" + email_add + "', '" + mail_add + "' , " + radio_group + "," + receive_info + ")";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/customers", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSqlnsert);
I've also add in the library for the JDBC in the library folder.
It seems the non numeric values aren't quoted
Instead of
" + email_add + ",
try
'" + email_add + "',
In the below insert statement
INSERT INTO table (field1, field2) VALUES ( 5, 'this is a string');
the 'this is a string' is wrapped in quotes, because field2 is of type VARCHAR. field1 is INT so the value assigned to it (5) doesn't need quoting.
The VALUES list must be in braces too and again, non numeric values must be quoted:
String strSqlnsert = "INSERT INTO Customers (MembershipID, Contact, Email, Address, SurveyStatus, Subscription) VALUES (" + member_ID + ", " + contact_num + ", '" + email_add + "', '" + mail_add + "', '" + radio_group + "', '" + receive_info + "')";
Try to make a prepared statement where you can call setInt or setString method of preparedStatement class . so it will automatically remove string or numeric confusion at all.
BTW preparedStatement is the best way to pass parameter in sql query.don't use Statement class for parameter sql query.

Java & Errors in my INSERT Statement

I need help on what I am doing wrong, please.
database called renters
addressID is auto-increment from a table called Property, and included in a table called Renter, I am doing a java assignment.
I connect to the database, and when I try to add a new renter it gives me this error
Error processing the SQL!java.sql.SQLException: Field 'AddressID' doesn't have a default value
Database connection terminated
here is my insert statement:
String addFirstName = txtFirstName.getText();
String addLastName = txtLastName.getText();
String addCellPhone = txtCellPhone.getText();
String addDepositPaid = txtDepositPaid.getText();
String addDepositAmtPaid = txtDepositAmtPaid.getText();
Statement lstatement = conn.createStatement();
ls_query = "INSERT INTO Renter (FirstName,LastName,CellPhone,DepositPaid,DepositAmtPaid) VALUES('" + addFirstName + "','" + addLastName + "','" + addCellPhone + "','" + addDepositPaid + "'," + addDepositAmtPaid + ")";
myStatement.executeUpdate(ls_query); JOptionPane.showMessageDialog(null, "New Renter Added");
;
Have a look at the database schema, probably column AddressID is marked as NOT NULL. You're trying to insert a record without specifying a value for that column, and as there's no default value specified the insert fails.
So, you can either modify the schema and remove the NOT NULL constraint if that makes sense, OR specify a default value for that column, OR provide one with your insert.
But there is another in my opinion major issue with your code: as you are stringing together the insert statement you're wide open to SQL injections. The solution is quite simple, use PreparedStatement, a good tutorial can be found here
Head over to bobby-tables.com, it's a great first introduction to the very frequent problem of SQL injection with recipes on how to avoid them for many languages, including Java. A great resource!
Add AddressID when using Insert command. In general every non default column and non AutoIncrement column must be inserted.
Try
ls_query = "INSERT INTO Renter (FirstName,LastName,CellPhone,DepositPaid,DepositAmtPaid,AdressId) VALUES('" + addFirstName + "','" + addLastName + "','" + addCellPhone + "','" + addDepositPaid + "','" + addDepositAmtPaid + "','0')";
EDIT
Im not experienced in java. But looks like the ; works like in C#.
A INSERT statement usually ends or delimits with ; in other words in MySQL says execute from this point.
Can you give this a try?
ls_query = "INSERT INTO Renter (FirstName,LastName,CellPhone,DepositPaid,DepositAmtPaid,AdressId) VALUES('" + addFirstName + "','" + addLastName + "','" + addCellPhone + "','" + addDepositPaid + "','" + addDepositAmtPaid + "','0');";

syntax error, I don't see one :S

Apparently i'm getting a syntax error, but I don't see any issues with my code!
if rs.EOF then
'User doesn't exist, create the record
query = currentDate&",'"
query = query + password & "','"
query = query + "user', '"
query = query + email & "', '"
query = query + fname & "', '"
query = query + sname & "'"
handle = add_to_database("users","jdate,password,perms,email,fname,sname",query)
response.write handle
else
response.write "Error: User already exists, please use a different email address"
end if
Here is the add_to_database function
function add_to_database(where,column,values)
'Create query
sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
on error resume next
'Add images to database
conn.Execute sql,recaffected
if err<>0 then
add_to_database = err.description &":"&sql&"<br />"
end if
'Destroy connection
sql = ""
end function
This is the error that is written to the page
Syntax error in INSERT INTO statement.:INSERT INTO users (jdate,password,perms,email,fname,sname) VALUES (#2013/8/2#,'af453d19feb2520c8c0d30fb39ebd211','user', 'martynleeba#gmail.com', 'Martyn', 'Ball')
The users table includes a field named password. But password is a reserved word, so can confuse the db engine when it finds it in a query. Rename that field if possible. If you must keep the name as is, enclosed it in square brackets to signal the db engine it is an object name.
handle = add_to_database("users","jdate,[password],perms,email,fname,sname",query)