updating in razor webmarix - razor

Line 22: var sql3 = "UPDATE productori SET Name3=#0, detail2=#1, harga2=#2 WHERE id=#3";
Line 23: var db2 = Database.Open("SmallBakery");<br>
Line 24: **db2.Execute(sql3 ,Name3, detail2, harga2);**
The bold area showing where the error is.
I have a problem with updating my database. I'm new with webmatrix and razor.
`Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if any) = ,Column name = Name3 ]`
That is the error that I get.
Below is my database format:
`ID = identity=big int
Name nvarchar
Description nvarchar
price nvarchar`
Help me understand what I did wrong please.

There is no column called Name3 in your database, but your SQL attempts to reference one with that name. Change the SQL so that the column names match what you have in the database. Also, you have 4 parameter markers in your SQL, but you only provide values for 3 of them so your SQL will never update any records.
Your SQL should look like this:
"UPDATE productori SET Name=#0, Description=#1, Price=#2 WHERE ID=#3";
Then you need to pass a value for the ID:
db2.Execute(sql3, Name3, detail2, harga2, variable_containing_id_value);

Related

Get Mysql table column names in app script

Is there any way to get the mysql table column names using a method like getColumnNames() in app script?
var conn = Jdbc.getConnection("jdbc:mysql://address/name", "username","password");
var stmt = conn.prepareStatement("SELECT * FROM bookings;");
var result = stmt.executeQuery();
var mat = [];
mat[0] = [];
mat.push(result.getColumnNames() );
getColumnNames() is not defind for Jdbc connection.
Is there any way to get the mysql table column names using a method like getColumnNames() in app script?
There's actually a function named getColumnName(column) from the JdbcResultSetMetaData. I think that's what you're looking for.
Here's additional info from the oracle docs where it's derived from.
getColumnName
String getColumnName(int column)
throws SQLException
Get the designated column's name.
Parameters:
column - the first column is 1, the second is 2, ...
Returns:
column name
Throws:
SQLException - if a database access error occurs

Insert Data to MYSQL using Foxpro

In FoxPro using native table, I usually do this when inserting new Data.
Sele Table
If Seek(lcIndex)
Update Record
Else
Insert New Record
EndIf
If I will use MYSQL as my DataBase, what is the best and fastest way to
do this in FoxPro code using SPT? I will be updating a large number of records.
Up to 80,000 transactions.
Thanks,
Herbert
I would only take what Jerry supplied one step further. When trying to deal with any insert, update, delete with SQL pass through, it can run into terrible debugging problems based on similar principles of SQL-injection.
What if your "myValue" field had a single quote, double quote, double hyphen (indicating comment)? You would be hosed.
Parameterize your statement such as using VFP variable references, then use "?" in your sql statement to qualify which "value" should be used. VFP properly passes. This also helps on data types, such as converting numbers into string when building the "myStatement".
Also, in VFP, you can use TEXT/ENDTEXT to simplify the readability of the commands
lcSomeStringVariable = "My Test Value"
lnANumericValue = 12.34
lnMyIDKey = 389
TEXT to lcSQLCmd NOSHOW PRETEXT 1+2+8
update [YourSchems].[YourTable]
set SomeTextField = ?lcSomeStringVariable,
SomeNumberField = ?lnANumericValue
where
YourPKColumn = ?lnMyIDKey
ENDTEXT
=sqlexec( yourHandle, lcSQLCmd, "localCursor" )
You can use SQL Pass through in your Visual Foxpro application. Take a look at the SQLCONNECT() or SQLSTRINGCONNECT() for connecting to your Database. Also look at SQLEXEC() for executing your SQL statement.
For Example:
myValue = 'Test'
myHandle = SQLCONNECT('sqlDBAddress','MyUserId','MyPassword')
myStatement = "UPDATE [MySchema].[Mytable] SET myField = '" + myValue + "' WHERE myPk = 1"
=SQLEXEC(myHandle, myStatement,"myCursor")
=SQLEXEC(myHandle, "SELECT * FROM [MySchema].[Mytable] WHERE myPk = 1","myCursor")
SELECT myCursor
BROWSE LAST NORMAL
This would be your statement string for SQLEXEC:
INSERT INTO SOMETABLE
SET KEYFIELD = ?M.KEYFIELD,
FIELD1 = ?M.FIELD1
...
FIELDN = ?M.FIELDN
ON DUPLICATE KEY UPDATE
FIELD1 = ?M.FIELD1
...
FIELDN = ?M.FIELDN
Notice that the ON DUPLICATE KEY UPDATE part does not contain the key field, otherwise it would normally be identical to the insert (or not, if you want to do something else when the record already exists)

MySQL Dynamic Query Statement in Python with Dictionary

Very similar to this question MySQL Dynamic Query Statement in Python
However what I am looking to do instead of two lists is to use a dictionary
Let's say i have this dictionary
instance_insert = {
# sql column variable value
'instance_id' : 'instnace.id',
'customer_id' : 'customer.id',
'os' : 'instance.platform',
}
And I want to populate a mysql database with an insert statement using sql column as the sql column name and the variable name as the variable that will hold the value that is to be inserted into the mysql table.
Kind of lost because I don't understand exactly what this statement does, but was pulled from the question that I posted where he was using two lists to do what he wanted.
sql = "INSERT INTO instance_info_test VALUES (%s);" % ', '.join('?' for _ in instance_insert)
cur.execute (sql, instance_insert)
Also I would like it to be dynamic in the sense that I can add/remove columns to the dictionary
Before you post, you might want to try searching for something more specific to your question. For instance, when I Googled "python mysqldb insert dictionary", I found a good answer on the first page, at http://mail.python.org/pipermail/tutor/2010-December/080701.html. Relevant part:
Here's what I came up with when I tried to make a generalized version
of the above:
def add_row(cursor, tablename, rowdict):
# XXX tablename not sanitized
# XXX test for allowed keys is case-sensitive
# filter out keys that are not column names
cursor.execute("describe %s" % tablename)
allowed_keys = set(row[0] for row in cursor.fetchall())
keys = allowed_keys.intersection(rowdict)
if len(rowdict) > len(keys):
unknown_keys = set(rowdict) - allowed_keys
print >> sys.stderr, "skipping keys:", ", ".join(unknown_keys)
columns = ", ".join(keys)
values_template = ", ".join(["%s"] * len(keys))
sql = "insert into %s (%s) values (%s)" % (
tablename, columns, values_template)
values = tuple(rowdict[key] for key in keys)
cursor.execute(sql, values)
filename = ...
tablename = ...
db = MySQLdb.connect(...)
cursor = db.cursor()
with open(filename) as instream:
row = json.load(instream)
add_row(cursor, tablename, row)
Peter
If you know your inputs will always be valid (table name is valid, columns are present in the table), and you're not importing from a JSON file as the example is, you can simplify this function. But it'll accomplish what you want to accomplish. While it may initially seem like DictCursor would be helpful, it looks like DictCursor is useful for returning a dictionary of values, but it can't execute from a dict.

Error while inserting perl variable in the mysql table on html page

Error while inserting email address in the table
**[perl] my $username=$CGI->{salesrep}; return $username;[/perl]#gmail.com**
I want to insert this value in the table.
But It gives null when executed.
[query type=list sql="INSERT tech4less.outofstock_sku SET name='[value name]',customer_email='[value email]', phone='[value phone]', state='[value b_state]', postalcode='[value zip]', country='[value country]', **salesperson='[perl]$username[/perl]#gmail.com**', product='[value wish_product]', item_description='[value wish_descrip]', manufacturer='[value wish_man]', category = '[value wish_cat]', business_yn='[value option]', date = now()"]
[/query]
If $CGI is a CGI object then access a parammeter this way:
my $username=$CGI->param('salesrep');
Read this: http://perldoc.perl.org/CGI.html#FETCHING-THE-VALUE-OR-VALUES-OF-A-SINGLE-NAMED-PARAMETER:
Regards

Matlab Database QueryDb error

I need to insert some data into a mysql database. Db is connected and working.
I am running the following code :
a = sprintf('%s',hashedStr);
sqlQueryStr = 'insert into products (security_code) values (a)'
QueryDB(sqlQueryStr);
I have a database called test and a table named products with 2 fields id and security_code.
When I run this, I get :
Unknown column 'a' in fieldlist ...
Why is this happening ? i dont have and dont need this column ...
Any help ?
Try with:
sqlQueryStr = sprintf('insert into products (security_code) values ("%s")',hashedStr);
QueryDB(sqlQueryStr);
problem is that you are not replacing "a" variable into sql expression