I am currently taking CS50, an online introductory course in coding by Harvard. We have just covered SQL and I am trying to attempt the question "Movies" in the problem set now, a description of which can be found here: https://cs50.harvard.edu/x/2020/psets/7/movies/
However, I am not sure how to do this correctly.
For example, for 1.sql, my code is as follows:
SELECT title FROM movies WHERE year = 2008;
I literally wrote only that one line and nothing more in the file "1.sql".
But when I run
$ cat 1.sql | sqlite3 movies.db
in my terminal, nothing happens?
Is this how I am supposed to write code for SQL? Or am I missing some stuff that I should be including (e.g. as headers or what) above my query?
To be clear, I believe I know how to write a query itself but I do not know the "protocol" to write it, if I may. I mean, for example, I am positively sure that "SELECT title FROM movies WHERE year = 2008;" fulfils the question's first requirement.
Some enlightenment would be appreciated!
EDIT 1
Okay first I must apologise to everyone who so very kindly took the time to comment on my post. For some very odd reason, my query did not return any results the first time I ran it. However, when I tried it again, it worked perfectly! Not sure what went wrong honestly, but all is well now! So sorry for wasting everyone's time ):
EDIT 2
Okay I also figured out that the reason why I could not execute my query is that I was in "sqlite3" in my terminal. I was supposed to run the command to execute my query in the main terminal i.e. not when it says "sqlite3". Stupid. I know.
Option 1) you can run "$ cat 1.sql | sqlite3 movies.db" in your normal terminal (not in the sqlite3 mode)
Option 2) you can open sql environment by typing "sqlite3 movies.db" (it will open movies.db if it exists, or create a temporary one if it doesn't exist). Then you can type ".read 1.sql" after the "sqlite>"
Open Browser and test it as mention on your course site
Usage
To test your queries on CS50 IDE, you can query the database by running
$ cat filename.sql | sqlite3 movies.db
where filename.sql is the file containing your SQL query.
Or you can paste them into DB Browser for SQLite’s Execute SQL tab and click ▶.
Related
Since I am new to Powershell I am looking for some assistance for my problem.
What I need is to count the number of files in a network folder and use that number for a Bulk-Insert loop. Currently, the number of loop cyles is stored in a "status-table" (in SQL). I want to be able to update that status table with the actual number of files.
The powershell cmdlet to count the number of files looks like this:
$FileCount = #(Get-ChildItem -Path '\\YS001UVE\Download\MIS\MIS FactData 20??m?1.csv' -Name -File | sort Name).count
But then I need to update the existing "status-table" with the outcome of the Powershell script .
What is the correct SQL coding for this?
Thanks in advance and regards
Peter
There are many ways to do that. This Post contains an example to do this task. For some database connections you may need to install something on your computer (for MySQL you need the MySQL Connector).
I've done this myself and know, how confusing it can be at first. Under this link, you can find my script (sorry for the German comments and text). The database connection starts at about line 270 in the function Import-ToDatabase. Be aware that I am not updating one single line in my script, but a whole new table.
Let me know if you need some more help
Here are a few more links:
https://www.joseespitia.com/2016/06/03/connecting-and-writing-to-a-sql-db-with-powershell/
https://www.sqlservercentral.com/scripts/insert-data-into-a-sql-server-table-using-powershell-using-invoke-sqlc
https://www.sqlservercentral.com/blogs/performing-an-insert-from-a-powershell-script
I'm not a DB person but something weird is happening in Access:
I'm trying to run this SQL command: UPDATE tbl_DirectorySet SET TempRootDir='D';
on this table:
It doesn't do anything! I mean the field is empty as before.
If I manually set the field with some value like "aaaaa", then after I run the SQL command I can see that the command worked as expected.
If you don't have any record in a cell, the UPDATE command won't work. Use INSERT instead. That's why it works when you first introduce "aaaaa" and then you UPDATE.
EDIT: this link may make you understand better the principles behind the basic SQL commands: https://en.wikipedia.org/wiki/Create,_read,_update_and_delete
I am attempting to tune our oracle database that has been running a little slowly lately.
I have generated a SQL trace file, and can run the basic TKPROF from the command prompt, and generate the appropriate output file.
tkprof.exe source.trc output.txt
I would very much like to see the execution plan as well since there are a good number of indexes that should be used with this database. To do this, I am trying to run this:
tkprof.exe source.trc output.txt EXPLAIN=mbw/password
The problem is, the username which every application uses to connect with is lowercase (mbw for this example, and I have to leave it this way). So whenever I wish to look at data, I have to put quotes around the user like this:
SELECT * FROM "mbw".TABLE1
Unfortunately, I can never seem to get TKPROF to connect as "mbw"/password, it is always as mbw/password, which will never work. (I can see what TKPROF is attempting to connect with in the output.txt file)
I have tried a bunch of permutations on the command line, and just can't seem to make it happen. I've tried things like:
... EXPLAIN="mbw"/password
... EXPLAIN=""mbw""/password
... EXPLAIN="""mbw"""/password
... EXPLAIN=^"mbw^"/password
Does anyone have any ideas on how to properly structure the TKPROF command so I can connect as a user with lowercase letters?
I fully apologize for my lack of good oracle and sql skills, I have been rather unexpectedly thrown into this particular job and am trying to learn as fast as I can.
I know how to do this in SQL Server thanks to this clever bit of code
Use <database>
SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script]
FROM sys.dm_exec_query_stats AS execquery
CROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsql
ORDER BY execquery.last_execution_time DESC
SOURCE:
Recover unsaved SQL query scripts
Is there a way to do this in Oracle SQL Developer?
This has saved my butt several times.
It is really a problem when you lose unsaved code. For about a
month I have been working on a big procedure, and forgot to save the
code in SVN. If you read this and remember have such unsaved code,
commit it immediately! :) Because everything could happen with your
test db. Ok. you're lucky if you were using Oracle SQL Developer,
because this program has a great feature - it saves your code in its
sql history inspite of eating a big party of your RAM. Open your file
explorer and locate this folder:
C:\Users\%USERNAME%\AppData\Roaming\SQL Developer\SqlHistory
You'll find a lot of XML files, and if you're twice lucky, you'll find
your lost code. It's wonderful. :) . If you're using another program,
try to find a feature like this and maybe it helps you. My condolences
if this post doesn't help you, in any case, try to find something good
among the next: 1) write your code again, and it will be better than
before as you did it once 2) commit your code, so you'll not face such
problem in the future
If you have the privileges then:
SELECT * FROM v$sql
If not then press F8 to bring up a list of previously ran queries.
You can try too, so you can get the unsaved SQL.
View > SQL History, for example, look this picture:
This is using SQLDeveloper's history, like in Matt's answer, but if you want to search through the history files for specific query fragments you remember, they are located as .xml files in /home/username/.sqldeveloper/SqlHistory. From there, enter:
find -type f -mtime -1 -print0 | xargs -0 grep -l <text>
(where -mtime -1 means no sooner than one day ago).
Through View> SQL History or by pressing F8 is a great way to do it, it lets you search for content or database connection, really a good implementation!
SQL History
Use the undo button, as shown in the image
Some of the queries I have written inside MS-Access are getting deleted automatically. And while I run the queries through code, I get this error:
Query should have one destination field
What can be the possible reason?
Explanation: I created a query in MS-access. Ran it from the code. Closed the database. Started it again, and now for that particular query, it is showing 'SELECT ;'only.
Strange. I am in panic mode now
Check if your query actually has any fields in the query design grid. When you open the query in design view, you will most likely notice it doesn't.
Does the query stick around when you don't run the code (but still close and reopen the database)?
If so, I would suspect that something in your code is overwriting the query.