My Table has the schema:
name: String - Nullable
Email: String - Nullable
Contact-number : String - Repeated
age: int64 Nullable
eg:
Name
Email
Contact-number
Age
John
row
1256545, 1309045
30
I want to update both the numbers of the user John.
update mytable
set Contact-number = ['0000','1111']
where name = 'John'
Related
I'm trying to update a specific row by ID :
fun updateEmployeeInfo(id:Int, firstName:String): Int {
val db = this.writableDatabase
var cv = ContentValues()
cv.put(COL_FIRSTNAME, firstName )
val result = db.update(TABLE_NAME, cv, COL_FIRSTNAME+"=?", arrayOf(firstName))
return result
}
Running this with an ID that already exists in the database it isn't updating.
screenshot of the database
You are saying update the rows with the provided (passed) first name by changing the first name to the very same first name (effectively doing nothing).
I believe that you want to use:-
val result = db.update(TABLE_NAME, cv, COL_ID+"=?", arrayOf(id.toString))
assuming that COL_ID holds the value of the id column name.
or you could use the more concise return db.update(TABLE_NAME, cv, COL_ID+"=?", arrayOf(id.toString))
This is then saying update the row where the id is the provided/passed id changing the first name, whatever it is, to the provided/passed first name.
I want to store the labels of each table in a dataset into a table containing (table_name, label_name, label_value) so that I can select table names with conditions on labels (for instance, a WHERE label_name = 'xxx' and label_value = 'yyy').
With this query, I can extract a stringified array of struct, each containing the label name and the label value:
*
FROM `mydataset.INFORMATION_SCHEMA.TABLE_OPTIONS`
WHERE option_name = 'labels'
Output looks like this:
WITH output AS
(
SELECT "table_name_a" as table_name, "ARRAY<STRUCT<STRING, STRING>>" AS option_type, "[STRUCT(\"label_name_a_1\", \"label_value_a_1\"), STRUCT(\"label_name_a_2\", \"label_value_a_2\"), STRUCT(\"label_name_a_3\", \"label_value_a_3\")]" as option_value
UNION ALL
SELECT "table_name_b" as table_name, "ARRAY<STRUCT<STRING, STRING>>" AS option_type, "[STRUCT(\"label_name_b_1\", \"label_value_b_1\"), STRUCT(\"label_name_b_2\", \"label_value_b_2\"), STRUCT(\"label_name_b_3\", \"label_value_b_3\")]" as option_value
)
SELECT * FROM output
The intuitive approach would be to cast to an array and specify the structure:
SELECT ARRAY<STRUCT<STRING, STRING>>[STRUCT("label_name_a_1", "label_value_a_1"), STRUCT("label_name_a_2", "label_value_a_2"), STRUCT("label_name_a_3", "label_value_a_3")]
but I couldn't find a way to do it "dynamically" for each option_value
A terribly bad solution would be to transform the string and then use a JSON_EXTRACT but I think there might be a much better and simpler way to do it?
Thanks!
Here's code sample to convert the option value string to ARRAY<STRUCT<key STRING, value STRING>> type:
DECLARE optionValue DEFAULT ((select option_value
FROM `zyun.INFORMATION_SCHEMA.TABLE_OPTIONS`
WHERE option_name = 'labels'
AND table_name = 'test'));
DECLARE label ARRAY<STRUCT<key STRING, value STRING>>;
EXECUTE IMMEDIATE "SELECT ARRAY<STRUCT<key STRING, value STRING>> " || optionValue INTO label;
SELECT label;
Output:
SELECT label; -- at [9:1]
+-----------------------------------------------------------+
| label |
+-----------------------------------------------------------+
| [{"key":"aaa","value":"bbb"},{"key":"ccc","value":"ddd"}] |
+-----------------------------------------------------------+
I have a query string which is
var projectid = sqlConn.Query<int>("insert into project (name,customer_id, service_id,user_id) values (#name,#customerid,#serviceid,#userid);SELECT SCOPE_IDENTITY(); ", new { name = Name, customerid = Customer, serviceid = Service, userid = userId }).Single();
where name is a nvarchar field. Now I know I need to use 'N' character and I have tried the followings
'N#name'
N'#name'
but it doesn't seem to work. Can anyone help me with this please?
You don't need to use N prefix with variables.
It is needed only with explicitly typed strings (string constants) like insert into ... values(N'some text', ...)
If your name variable contains unicode string - then no special actions are required.
I have Ticket table that has some columns like this :
ID : int
Body : nvarchar
Type : int
I have many rows where the Body column has value like this :
IPAddress = sometext, ComputerName = sometext , GetID = sometext, CustomerName=sometext-sometext , PharmacyCode = 13162900
I want update all rows' Type column where the Body column has at least five of the following keys:
IPAddress, ComputerName, GetID, CustomerName, PharmacyCode
You could do it with a simple update statement like that
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%'
and Body LIKE '%ComputerName%'
and Body LIKE '%GetID%'
and Body LIKE '%CustomerName%'
and Body LIKE '%PharmacyCode%'
if you know the 'keys' are always in the same order you could concatenate the LIKE conditions like so
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%ComputerName%GetID%CustomerName%PharmacyCode%'
If you have the possibility to change the data model it would be much better to explode this key & value column into an own table and link it back to this table as it is done in a proper relational model.
If you could calculate number of key value pair by number of = present in your string you could use this query
Update tblname set col=val where len(colname) - len(replace(colname,'=','')>5
The where part actually gives number of equal signs present in your string.
Suppose I want to check if some string appears as name-surname in the concatenation of two rows name and surname of a table. How do I write valid this sql in a rails style ? And is these syntaxes correct ?
SELECT (name + '-'+ surname) FROM table1 where (name + '-'+ surname = string)
table.select(:name+'-'+:surname).where((:name+'-'+:surname) == string)
I am not sure if I am understanding your question correctly, but I think this is what you are wanting. For the following string variable,
string = "John - Doe"
you want to pull a record like this from the User table
id | name | sur_name
1 | John | Doe
If this is what you want, you can actually massage your string variable like this
parsed_string = string.split('-')
name = parsed_string[0].strip # strip to remove white spaces
sur_name = parse_string[1].strip
Then, you can run the following code to get what you want:
users = User.where(:name => name, :surname => sur_name)
Hope this answers your question.