I'm having an issue with SQL in python, I am simply creating a table and I want to print it out. I'm confused D: I am using python 3.x
import sqlite3
print ("-" * 80)
print ("- Welcome to my Address Book -")
print ("-" * 80)
new_db = sqlite3.connect('C:\\Python33\\test.db')
c = new_db.cursor()
c.execute('''CREATE TABLE Students
(student_id text,
student_firstname text,
student_surname text,
student_DOB date,
Form text)
''')
c.execute('''INSERT INTO Students
VALUES ('001', 'John','Doe', '12/03/1992', '7S')''')
new_db.commit()
new_db.close()
input("Enter to close")
firstname = "John"
surname = "Foo"
c.execute("SELECT * FROM Students WHERE student_firstname LIKE ? OR student_surname LIKE ? ", [firstname, surname])
while True:
row = c.fetchone()
if row == None:
break# stops while loop if there is no more lines in table
for column in row: #otherwise print line from table
print col, " "
output should be:
001 John Doe 12/03/1992 7S
and any other row from table that contain first name John OR surname Foo
Related
An address field in a Progress database evidently is multiple lines. So if I do something simple like:
for each customer where customer-number = "123" no-lock.
display customer-number
customer-name
customer-address.
I get results of:
123 John Smith 123 Easy St.
c/o Jane Smith
Apartment C12
I want results of:
123 John Smith 123 Easy St. c/o Jane Smith Apartment C12
I've tried Entry - but get an error if an element does not exists. I am trying to export to CSV and want each line to be another column and if a column does not exists then it is blank.
Any help will be MUCH appreciated!
If the delimiter is a newline then something like this should work:
define variable n as integer no-undo.
define variable i as integer no-undo.
define variable address as character no-undo.
output to value( "customer.csv").
for each customer no-lock:
address = "".
n = num-entries( customer-address, "~n" ).
do i = 1 to n:
address = address + entry( i, customer-address, "~n" ) + " ".
end.
export delimiter "," customer-number customer-name trim( address ).
end.
output close.
or, if you just need to change the delimiter within the customer-address field:
output to value( "customer.csv").
for each customer no-lock:
export delimiter "," customer-number customer-name replace( customer-address, "~n", "," ).
end.
output close.
(And, actually, my original code is really just replacing ~n with a space so you could do that too...)
I would like to repeat this command as many times as there is still sometextin the field note (several rows from the table itemNotes could have one or more sometext in the field note):
UPDATE itemNotes
SET
note = SUBSTR(note, 0, INSTR(LOWER(note), 'sometext')) || 'abc' || SUBSTR(note, INSTR(LOWER(note), 'sometext')+sometext_len)
WHERE
INSTR(LOWER(note), 'sometext') >= 0;
So a proto-code would be :
While (SELECT * FROM itemNotes WHERE note like "%sometext%") >1
UPDATE itemNotes
SET
note = SUBSTR(note, 0, INSTR(LOWER(note), 'sometext')) || 'abc' || SUBSTR(note, INSTR(LOWER(note), 'sometext')+sometext_len)
WHERE
INSTR(LOWER(note), 'sometext') >= 0;
END
But apparently Sqlite3 doesn't support While loop or for loop. They can be emulated with something like this but I have difficulties integrating what I want with this query:
WITH b(x,y) AS
(
SELECT 1,2
UNION ALL
SELECT x+ 1, y + 1
FROM b
WHERE x < 20
) SELECT * FROM b;
Any idea how to do this?
PS: I don't use replace because I want to replace all the case combinations of sometext (e.g. sometext, SOMEtext, SOmeText...) cf this question
Current input and desired output:
For a single row, a note field could look like (and many rows in the table itemNotescould look like this one):
There is SOmetext and also somETExt and more SOMETEXT and even more sometext
The query should output:
There is abc and also abc and more abc and even more abc
I am doing it on the zotero.sqlite, which is created by this file (line 85). The table is created by this query
CREATE TABLE itemNotes (
itemID INTEGER PRIMARY KEY,
parentItemID INT,
note TEXT,
title TEXT,
FOREIGN KEY (itemID) REFERENCES items(itemID) ON DELETE CASCADE,
FOREIGN KEY (parentItemID) REFERENCES items(itemID) ON DELETE CASCADE
);
You just have your answer in your query:
UPDATE itemNotes
SET
note = SUBSTR(note, 0, INSTR(LOWER(note), 'sometext')) || 'abc' || SUBSTR(note, INSTR(LOWER(note), 'sometext')+sometext_len)
WHERE
note LIKE "%sometext%";
It will update all rows that contain sometext in the note field
UPDATE
If you want to update the field which has multiple occurrences in different cases and maintain the rest of the text the simplest solution imo is to use regex and for that you need an extension
UPDATE itemNotes
SET
note = regex_replace('\bsometext\b',note,'abc')
WHERE
note LIKE "%sometext%";
As recommended by Stephan in his last comment, I used python to do this.
Here is my code :
import sqlite3
import re
keyword = "sometext"
replacement = "abc"
db = sqlite3.connect(path_to_sqlite)
cursor = db.cursor()
cursor.execute(f'SELECT * FROM itemNotes WHERE note like "%{keyword}%"')
for row in cursor.fetchall():
row_regex = re.compile(re.escape(keyword), re.IGNORECASE)
row_regex_replaced = row_regex.sub(replacement, row[2])
rowID = row[0]
sql = "REPLACE INTO itemNotes (itemID,note) VALUES (?,?)"
data = (rowID, row_regex_replaced)
cursor.execute(sql, data)
db.commit()
I need to read a 10GB fixed width file to a dataframe. How can I do it using Spark in R?
Suppose my text data is the following:
text <- c("0001BRAjonh ",
"0002USAmarina ",
"0003GBPcharles")
I want the 4 first characters to be associated to the column "ID" of a data frame; from character 5-7 would be associated to a column "Country"; and from character 8-14 to be associated to a column "Name"
I would use function read.fwf if the dataset was small, but that is not the case.
I can read the file as a text file using sparklyr::spark_read_text function. But I don't know how to attribute the values of the file to a data frame properly.
EDIT: Forgot to say substring starts at 1 and array starts at 0, because reasons.
Going through and adding the code I talked about in the column above.
The process is dynamic and is based off a Hive table called Input_Table. The table has 5 columns: Table_Name, Column_Name, Column_Ordinal_Position, Column_Start, and Column_Length. It is external so any user can change, drop, and remove any file into the folder location. I quickly built this from scratch to not take actual code, does everything make sense?
#Call Input DataFrame and the Hive Table. For hive table we make sure to only take correct column as well as the columns in correct order.
val inputDF = spark.read.format(recordFormat).option("header","false").load(folderLocation + "/" + tableName + "." + tableFormat).rdd.toDF("Odd_Long_Name")
val inputSchemaDF = spark.sql("select * from Input_Table where Table_Name = '" + tableName + "'").sort($"Column_Ordinal_Position")
#Build all the arrays from the columns, rdd to map to collect changes a dataframe col to a array of strings. In this format I can iterator through the column.
val columnNameArray = inputSchemaDF.selectExpr("Column_Name").rdd.map(x=>x.mkString).collect
val columnStartArray = inputSchemaDF.selectExpr("Column_Start_Position").rdd.map(x=>x.mkString).collect
val columnLengthArray = inputSchemaDF.selectExpr("Column_Length").rdd.map(x=>x.mkString).collect
#Make the iteraros as well as other variables that are meant to be overwritten
var columnAllocationIterator = 1
var localCommand = ""
var commandArray = Array("")
#Loop as there are as many columns in input table
while (columnAllocationIterator <= columnNameArray.length) {
#overwrite the string command with the new command, thought odd long name was too accurate to not place into the code
localCommand = "substring(Odd_Long_Name, " + columnStartArray(columnAllocationIterator-1) + ", " + columnLengthArray(columnAllocationIterator-1) + ") as " + columnNameArray(columnAllocationIterator-1)
#If the code is running the first time it overwrites the command array, else it just appends
if (columnAllocationIterator==1) {
commandArray = Array(localCommand)
} else {
commandArray = commandArray ++ Array(localCommand)
}
#I really like iterating my iterators like this
columnAllocationIterator = columnAllocationIterator + 1
}
#Run all elements of the string array indepently against the table
val finalDF = inputDF.selectExpr(commandArray:_*)
I have written a query which allows a user to search a table in an sqlite3 database with any of 4 different fields. It will only return data if what the user enters is an exact match is there a way to put a wild card in for each field. Code below
import sqlite3
conn = sqlite3.connect("lucyartists.db")
cursor = conn.cursor()
#Get user input
print ('Search for an Artist')
print ('You can search by Name, DOB, Movement or Description')
print ('')
name = input("Enter Artists Name: ")
dob = input("Enter Date of Birth: ")
movement = input("Enter Movemment: ")
description = input ("Enter Description: ")
#query database with user input
sql = ("select name, dob, movement, description from artists where name like (?) or dob like (?) or movement like (?) or description like (?)")
values = (name, dob, movement, description)
#Run query
cursor.execute(sql, values)
result = cursor.fetchall()
#print results without quotes and comma separated
print()
print('RESULTS')
for r in result:
print()
print("{}, {}, {}, {}".format(r[0], r[1], r[2], r[3]))
I am not really sure why this is not working. When I try to run the any part of the loop I get sqlite3.OperationalError: near "WHERE": syntax error.
con = sqlite3.connect('DatabaseName.sql')
cur = con.cursor()
if changes == "1":
Fname = input("Enter new first name: ")
Lname = input("Enter the last name of the person whom first name you wish to change: ")
cur.execute("""UPDATE Contacts SET Fname WHERE Lname""")
con.commit()
elif changes == "2":
Lname = input("Enter new last name: ")
Fname = input("Enter the first name of the person whom last name you wish to change: ")
cur.execute("UPDATE Contacts SET Lname WHERE Fname")
con.commit()
elif changes == "3":
Phone = input("Enter new telephone number(no dashes or spaces): ")
Fname = input("Enter the first name of the person whom telephone number you wish to change: ")
Lname = input("Enter the last name of the person whom telephone number you wish to change: ")
Phone = int(Phone)
cur.execute("""UPDATE Contacts SET Phone WHERE Fname AND Lname""")
con.commit()
Python and SQL are two completely indepent languages; you cannot directly access variables of one from the other.
To pass the values of Python variables into your SQL commands, use parameters, like this:
cur.execute("UPDATE Contacts SET FirstName = ? WHERE LastName = ?", (Fname, Lname))
(Here, FirstName and LastName are the column names in the table, and may or may not be identical with the Python variable names.)