So im having some trouble with my database right now, I can't seem to update a variable, Im just looking to do a simple "UPDATE" query, this is what it looks like:
MY table
So very simple table and all I am looking to do is update the "Sent" column from no to yes.
I've been using UPDATE but for some reason this dosn't work, no syntax error just says 0 rows affected and nothing changes...
UPDATE `numbers` SET `Sent`='True' WHERE `fName`='John';
(Im hoping to change the sent value of all rows containing fName = John.... As you can see in my table John is a value in the database so this should work but for some reason doesn't) Could anyone explain why my statement is wrong and what I am not doing right?
You need to debug this. Start with the select query:
SELECT n.*
FROM numbers n
WHERE fName = 'John';
This should return no rows -- meaning that what you see is not what you got. One common problem are invisible characters at the beginning, end, or both (often just spaces). So you can try:
WHERE fName LIKE '%John'
WHERE fName LIKE 'John%'
WHERE fName LIKE '%John%'
Once you figure out what works, you can figure out what to use in the UPDATE or how to fix the data.
Related
Got a apparently simple problem resolvable.
I want to update emails in my sql table with one query.
All emails ending by #notgoodexample.com (knowing I have several pattern that my email will match)
Should after that query be #goodexample.com
And I need to keep what is before the '#'.
My best try for the moment is
Update tableName SET table.email_addresse = concat(table.name, table.lastname, '#goodexample.com' --cheating a little bit cause these addresses are name.lastname#
WHERE email_address LIKE '%pattern%'
OR email_address LIKE '%pattern2%'
Do you have any tips for me ?
I have an excel spreadsheet with 15 or so fields. Wat I'm doing is, i open it, grab a row of data, then check each row for a value. Then using a few criteria I go look up to see if this Client value is already in the DB. As Example
Some of the fields mind be empty. Basically after checking some of the fields, I use them to check if that record exists already in DB. the problem arises when some fields are empty in which case when I query sql server it looks something like...
Select * from TblA where Company='Apple' and CompanyAdd ='Cupertino' and City=''
Because City = '' - it doesnt not find anything in SQL. The only thing that works is
and City is NULL
How am I able to programmatically assign that to a variable like CITY?
it is a string and the field in SQL is varchar
EDIT:
I want to be able to do something like this..... (as example)
if city = "" then
'I need this here to be so that....
city IS NULL
End if
So that when I query db it looks something like...
Select count(*) from TblA where City is Null
Is somethng like that possible?
You can use COALESCE for this purpose.
SELECT *
FROM TblA
WHERE COALESCE(Company, '')='Apple'
AND COALESCE(CompanyAdd, '') = 'Cupertino'
AND COALESCE(City, '') = ''
Keep in mind that the performance of this query will most likely not be stellar.
I am having trouble returning a single value from a database using SQLite3 and Ruby.
A bit of context: I have a table of people and I would like to be able to query the database and get back the first name of someone in that table. I am using Rubymine with the SQLite3 gem.
When I run this code:
name = #db.execute <<-SQL
SELECT firstname FROM tbl_people WHERE email_address = '#{email_address}';
SQL
puts name
I get the following ouput:
{"firstname"=>"james", 0=>"james"}
When really the output I am expecting is:
james
If anyone can even point me in the right direction here it would be very much appreciated.
Cheers,
James
The execute() method creates a resultset, not just a single scalar value. You need to process the resultset to get the single value out of it.
name.each do |row|
puts row['firstname']
end
It sounds like you want to return the first value of the first row of a result set, and discard all other values and rows. The Database#get_first_value is what you want.
Also, for security you shouldn't inject strings into your SQL; allow the library to handle that for you.
query = "SELECT firstname FROM tbl_people WHERE email_address = ?;"
name = #db.get_first_value query, email_address
puts name
I have a table and one of the columns holds web addresses like: 'http://...' or 'https://...'.
The problem is that there are some invalid entries, like 'shttp://...' or '#http//...' (the first character is invalid) and I want to correct all of them.
I use the following SQL statement:
'SELECT [...] FROM MyTable WHERE WebAddress LIKE '_http%'
and I successfuly get the problematic rows.
But how am I going to change/correct all of them using an UPDATE statement?
If you have some other solution please share it!
Simply change the SELECT to an UPDATE (of course, with some syntax changes) with a "fix" expression
UPDATE
MyTable
SET
WebAddress = SUBSTRING(WebAddress, 2, 8000)
WHERE
WebAddress LIKE '_http%'
You Can use Sub string property as you can trim odd letters .Also like '_word start' suitable for your question
I have the same copy of access running in 3 cities right now. They work perfectly ok. They are 99% the same with one minor difference. Each of them has two views which use different odbc connection to different cities DB (all these databases are SQL Server 2005). The views act as datasource for some two very simple queries.
However, while I tried to make a new copy for a new city, I found that one of the simple internal query returns the correct number of row but all data are empty while the other query functions correctly.
I checked the data of these two views, the data is correct.
The one causing problem are like
Select * from View_Top where Name = "ABC"
when the recordset returns, even rs!Name give me an empty string.
Please help
Well the query looks a little wrong to me, try using ' instead of " to delimit your ABC string...
Without the definition of VIEW_TOP it's hard to tell where your error is, but if you're getting rows but the columns are NULL I'm guessing that VIEW_TOP (or something it depends on) includes an OUTER JOIN and you're pulling the columns from the wrong side of the JOIN.
SELECT
acc.FIRM,
acc.OFFICE,
acc.ACCOUNT,
a.CONV_CODE,
a.OTHER_AMT AS AMOUNT,
a.TRANS_DATE,
a.DESCRIPTN,
a.JRNAL_TYPE
FROM AccTrans AS a LEFT OUTER JOIN ACC AS acc ON a.ACCNT_CODE = acc.ACCNT_CODE
WHERE
(acc.SUN_DB = 'IF1') AND
(ANAL_T0 <> '') AND
(a.TRANS_DATE < '20091022') AND
(a.JRNAL_TYPE = 'MATCH');
This is the definition of the view. Indeed, in Access i am able to view the result of this query, it has data. that's why i know the recordset returns the correct number of row (by counting the loop in the code). sorry for my mistakes, i use Account in the where clause, the select statements should be like
select Firm, Office, Account, Trans_Date.... from
view_top
where account = 'ABC'
the query returns the right number of row but all row data (even the account field) are empty string.
then i found out what really cause the problem is the AMOUNT field, if i omit the amount, everything works. though i don't understand why.
view_top definition
"Name, Account, AccountCode, Amount, Date...."
Select Statements:
Select Name, Account, AccountCode, Amount, Date
From View_Top Where Name = 'xxx'
I found that if I omit the Amount, everything works.
Though I still don't understand why.