Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using the command-line tools

From: SQLite 3 with PHP Essential Training

Video: Using the command-line tools

In this movie, I am going to show you how to use the SQLite 3 Command Line tool on both the Mac and the PC. First, we'll start with the Mac. So we are going to talk a little bit about using the SQLite 3 Command Line application on a Mac. We are going to open the Terminal application, because that's how we get to the command line on a Mac, and this will put us with a command line in my Home directory here, and so it will be your Home directory on your Mac. So I am going to try not to assume that you know very much about the command line. In UNIX, this is the Bash shell, which is a default on a Mac, and the commands here are very similar to what you might find in most UNIXes.

Using the command-line tools

In this movie, I am going to show you how to use the SQLite 3 Command Line tool on both the Mac and the PC. First, we'll start with the Mac. So we are going to talk a little bit about using the SQLite 3 Command Line application on a Mac. We are going to open the Terminal application, because that's how we get to the command line on a Mac, and this will put us with a command line in my Home directory here, and so it will be your Home directory on your Mac. So I am going to try not to assume that you know very much about the command line. In UNIX, this is the Bash shell, which is a default on a Mac, and the commands here are very similar to what you might find in most UNIXes.

In particular, this is based on BSD UNIX, and if you're familiar with UNIX at all, it should all be very, very familiar. So the first thing we are going to do is we are going to make a symbolic link, because getting to the exercise files in the XAMPP directory is a little bit circuitous on a Mac. So I'm going to type 'ln,' which is the command that will make a symbolic link, and that's a lowercase l and a lowercase n; UNIX command line is case-sensitive. I am going to say -s, which will make it a symbolic link, and then we are going to navigate to the applications folder, so I am going to type a slash and a capital A and maybe a couple of ps and hit the Tab key, and it will complete that for me, so I don't have to type it all out and perchance misspell it, and then a capital X and a capital A, and that's probably enough for XAMPP.

I will hit the Tab again, and it will complete that. And then HT, and that's probably enough, hit Tab. You'll notice it doesn't put the slash in for me there because that itself is a symbolic link. So I am going to put in the slash for it, and then a capital E, lowercase x, and that should be enough for our exercise files. Now I am going to backspace over that last slash because I don't really want that in my symbolic link, and press the Spacebar and the period. And so you should have a command that looks exactly like this. And then press Enter, and that has now created a symbolic link.

So if I bring up a listing of my directory, which is with ls - a, like that, you'll see that I have ExerciseFiles right here, and that is a symbolic link. So I am going to cd into that, and again, I can just type a couple of letters, and the Tab, and now ls-al, and I can see my entire ExerciseFiles folder right here. That makes it easy for me to get into my ExerciseFiles folder. So now I am going to navigate from here into the SQL folder, and we will look in there, and we see that we have these SQL files and these db files.

The db files are already made database files, and the SQL files are actually the source SQL for creating those files. So one of the things we are going to do here is we are going to use the command line application to create one of these db files, just so we can see how that's done. So I am going to type 'sqlite 3,' like that, all lowercase. So this is the command line application that comes with SQLite 3 and is designed for managing these databases. So if I give it the name of a database, say like test.db, now I have that database open. And if I want to see what's in it, I can type select * from sqlite_master; all SQL statements are terminated with a semicolon, and there is the schema for this database.

It's got a table named customer, and here is the create statement that made that table, and here is the one that created the item table, and here's one that created the sale table. So we have all that in there, and if I want to say select * from customer;, then I get the contents of that. At anytime, you can type help with a dot in front of it, .help. You have to scroll back to see the whole thing. We get all the dot commands that are available, plus you can just type any SQL directly into the command line application.

So I am going to Exit and type .quit, to get out of this and show you one other way to use this. If I want to type sqlite3, like that, and give it the name of a database, I can then just put a command in quotes. I can say, select * from customer, and I get the listing right there, without having to go into the application and type it on the command line inside the application. You can also redirect commands from a file. So if I want to create a new file that's just like test.db, I can say sqlite3 newtest.db and use the redirect operator from the command line, which is the left angle bracket.

If you press Shift on the Comma key, you will get that left angle bracket, at least on an American keyboard, and then type test-sqlite3.sql, and this is the source SQL for the test.db file. So if I do that, it has now created a new database file. If I look at the listing of the directory here, we have this new test.db. That's a new file created from the test-sqlite3 sql. So we can look inside that test-sqlite3. sql and use the less command, and we can see what's in there.

I am scrolling with j. You can scroll with j and k in here and just press q to get out of it, and so you can see all the SQL in there that's used to create the test.db. So that's how you use the SQLite 3 command line application on a Mac. Now I am going to show you how to use the SQLite 3 command line utility on the PC. First, we are going to locate the utility, and so I am going to open my Computer and go in the C drive, and I have installed XAMPP in the default location, and so I am going to go in the apache directory.

This is not intuitive where it is. And it's in the bin directory under the apache directory. Scroll down here, and there it is. So what I want is this path up here. So I am going to do my little trick here that works on Windows 7 - I believe it also works on Windows Vista, where I put my mouse in this little box up there, and it will give me that path, and I can just press Ctrl+C and copy it. And this is going to come in really handy here in a minute. Now I am going to right-click on the desktop and say New > Shortcut and type 'cmd.exe,' and that will give me a shortcut to a command shell.

When I press Enter, it will ask me for a name, and I am just going to say, SQL, because what this is going to be is it's going to be a command prompt in the folder that has the default SQL stuff in it. Press Enter for finish, and now we have that. But before you run it, I'm going to right-click on that shortcut and bring up Properties. I am going to do a couple of things first here. I am going to just move this out of the way, and I am going to get another Explorer window, that's computer over here, and I am going to go to my ExerciseFiles and click on SQL, and bring my cursor up there and copy that path.

And that's the path that I am going to paste in here. So the Start in directory is going to be there, because that's where we have our SQL stuff, and that's where we're going to want the command line utility. So I will say OK, and now we have this shortcut that brings us right there, and there is that directory. So I will quit that, and then I am going to put this back in my Copy buffer. And now I am going to add this to the command execution path, so I can just type sqlite3, and it will bring that up. So I am going to go to my Control panel, and System and Security, and down here to System. Then I am going to click Advanced system settings.

For some reason, that's where they stuck this. There is a button down here that says Environment Variables, and I am going to scroll down here to the one that says Path, and I am just going to double-click on that. And then all the way out at the end, I am pressing my End key, I press a semicolon and paste my copy buffer there. Press OK and OK and OK and red X. And now when I bring this up, I am there in my SQL directory, and I can type 'sqlite3' and press Enter, and I have got the SQLite command line utility.

So all that setup stuff, you don't ever need to do that again; all you need to do is double-click on this shortcut icon, and type in sqlite3 up here, and there it is. Now in this utility, you can type .help, and you get a full bunch of help here. I can scroll up and see all of that, and that's all the dot commands that work inside the SQLite 3 command line utility. I am going to type '.quit' and get out of it, and dir, and we see our databases.

So the way this works is you type sqlite3 and the name of a database. So in this case I am going to bring up the test.db database and press Enter, and that's been open now and just select * from sqlite master, sqlite_master; and so there is the sqlite master pseudo table for the test.db database. So if I want to, I can say select * from customer;, and I get the customer table, and .quit.

I can also do this from the command line without actually going into the utility interactive mode. I can say sqlite3 test.db and then put in quotes "select * from customer;" and I can get it right there as well. One more thing that I can do that's useful with this, if I want to create a new database, what I have here is I have these little SQL scripts, if I bring one of these up in my editor, you can see here's test sqlite3.

You see this is the actual script that created the database. And if I want to recreate the database, I can say sqlite3, and give it a new name, so testworking.db and redirect from, which is the left angle bracket and then the name of the file, so test-sqlite3.sql, and now I have created a new database, test-working.db, and that one will work just like the other one.

Sqlite3test-working.db and "select * from customer;" and there we have the same data. So I am just going to delete that file, test-working.db, and that's gone now. In this movie, I have shown you how to use the SQLite 3 command line utility on both the PC and the Mac, so that you're able to work with your databases from the command line, and even recreate the databases from the raw SQL, should you need to do that.

Show transcript

This video is part of

Image for SQLite 3 with PHP Essential Training
SQLite 3 with PHP Essential Training

98 video lessons · 8194 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 44s
    1. Welcome
      1m 26s
    2. What is SQLite and what are the prerequisites?
      1m 49s
    3. Using the exercise files
      2m 29s
  2. 41m 36s
    1. Getting the most out of the quick start
      4m 8s
    2. Creating and using a database
      10m 33s
    3. Inserting data into a table
      8m 17s
    4. Getting data from a table
      4m 38s
    5. Updating data in a table
      4m 31s
    6. Deleting data from a table
      4m 12s
    7. Creating a database library
      5m 17s
  3. 39m 19s
    1. Installing XAMPP on a Mac
      5m 59s
    2. Installing XAMPP on a PC
      4m 45s
    3. Setting up SID and exercise files on a Mac
      9m 10s
    4. Setting up SID and exercise files on a PC
      8m 12s
    5. Using the command-line tools
      11m 13s
  4. 30m 44s
    1. Understanding databases and tables in SQLite
      2m 34s
    2. Creating a database
      5m 45s
    3. Setting permissions for a database
      5m 24s
    4. Defining a table in SQL
      2m 21s
    5. Creating a table in PHP
      3m 34s
    6. Creating indexes
      4m 38s
    7. Indexing ID fields
      6m 28s
  5. 36m 35s
    1. Taking care with data types in SQLite
      9m 33s
    2. Storing numbers with INTEGER
      5m 17s
    3. Storing numbers with REAL
      2m 15s
    4. Storing text
      3m 15s
    5. Storing large data with BLOB
      9m 31s
    6. Storing booleans
      2m 24s
    7. Storing dates and times
      4m 20s
  6. 22m 47s
    1. Storing data with INSERT
      4m 1s
    2. Updating data with UPDATE
      3m 12s
    3. Reading data with SELECT
      6m 14s
    4. Reading data from multiple tables with JOIN
      5m 29s
    5. Deleting data with DELETE
      3m 51s
  7. 24m 45s
    1. Understanding expressions in SQLite
      2m 31s
    2. Comparing values with comparison operators
      4m 36s
    3. Matching patterns with LIKE
      4m 28s
    4. Building simple math with arithmetic operators
      3m 13s
    5. Matching values in a list with IN
      2m 22s
    6. Choosing from multiple conditions with the CASE expression
      4m 45s
    7. Forcing a data type with CAST
      2m 50s
  8. 24m 38s
    1. Finding the LENGTH of a string
      1m 52s
    2. Changing case with UPPER and LOWER
      2m 15s
    3. Reading parts of a string with SUBSTR
      2m 34s
    4. Changing parts of a string with REPLACE
      1m 17s
    5. Trimming blank spaces with TRIM
      1m 59s
    6. Reading absolute values with ABS
      1m 6s
    7. Rounding values with ROUND
      1m 42s
    8. Finding the data type of an expression with TYPEOF
      1m 14s
    9. Finding the last inserted record with LAST_INSERT_ROWID
      1m 7s
    10. Getting the version of your SQLite library
      1m 16s
    11. Creating user-defined functions
      4m 51s
    12. Building aggregate user-defined functions
      3m 25s
  9. 18m 58s
    1. Understanding aggregate functions
      2m 15s
    2. Counting rows with COUNT
      3m 30s
    3. Building with the SUM and TOTAL functions
      2m 9s
    4. Finding minimum and maximum values with MIN and MAX
      4m 33s
    5. Finding averages with AVG
      1m 50s
    6. Grouping results with GROUP BY
      2m 23s
    7. Selecting for aggregation with HAVING
      2m 18s
  10. 13m 54s
    1. Understanding SQLite support for dates and times
      4m 31s
    2. Getting readable, sortable dates and times
      3m 36s
    3. Getting high-resolution dates and times with JULIANDAY
      2m 49s
    4. Formatting dates and times with STRFTIME
      2m 58s
  11. 18m 7s
    1. Understanding collation
      1m 0s
    2. Sorting results with ORDER BY
      1m 30s
    3. Removing duplicate results with DISTINCT
      2m 5s
    4. Understanding indexes
      3m 18s
    5. Working with primary key indexes
      4m 12s
    6. Understanding how to use the INTEGER PRIMARY KEY function
      6m 2s
  12. 7m 52s
    1. Understanding transactions
      1m 10s
    2. Using transactions in SQLite
      6m 42s
  13. 16m 6s
    1. Understanding subselects
      1m 29s
    2. Creating a simple subselect
      3m 18s
    3. Searching within a result set
      2m 35s
    4. Searching within a joined result
      3m 59s
    5. Creating a view
      2m 17s
    6. Searching within a joined view
      2m 28s
  14. 15m 56s
    1. Understanding triggers in SQLite
      52s
    2. Automatically updating a table with a trigger
      5m 18s
    3. Logging transactions with triggers
      2m 41s
    4. Improving performance with triggers
      2m 3s
    5. Preventing unintended updates with triggers
      2m 50s
    6. Adding automatic time stamps
      2m 12s
  15. 16m 25s
    1. Choosing an interface
      1m 21s
    2. Using the SQLite3 interface
      2m 41s
    3. Using the PDO interface
      3m 0s
    4. Creating a library
      9m 23s
  16. 16m 33s
    1. Defining CRUD
      4m 5s
    2. Using PHP to open and use an SQLite database
      3m 36s
    3. Using PHP to insert into an SQLite database
      4m 18s
    4. Using PHP to read from an SQLite database
      2m 20s
    5. Using PHP to update an SQLite database
      1m 2s
    6. Using PHP to delete from an SQLite database
      1m 12s
  17. 11m 32s
    1. An overview of the testimonials application
      4m 48s
    2. Managing the database in PHP
      4m 5s
    3. Displaying the testimonials using PHP
      2m 39s
  18. 32s
    1. Goodbye
      32s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQLite 3 with PHP Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.