SQLite is a self-contained, serverless and transactional SQL database engine. It is used in many other applications that need a lightweight, embedded database. In this article, we are going to see how we can insert a value that contains an apostrophe in SQLite using various examples and so on

Understanding the Issue with Apostrophes

The most probable case with the need to use apostrophes is when handling the string types. Before understanding how to deal with apostrophes let’s understand what string type means and how is it related to apostrophes.

Strings are a datatype of SQLite. There are various types of string available in SQLite some of them are as follows:

  • CHARACTER(20)
  • VARCHAR(255)
  • VARYING CHARACTER(255)
  • NCHAR(55)
  • NATIVE CHARACTER(70)
  • NVARCHAR(100)
  • Let us create a sample table to see how to insert string values.

    CREATE TABLE test (
    name VARCHAR2(15),
    id INT
    );

    Now let us insert string values into it. To do that we will surround the values in quotes.

    INSERT INTO test VALUES ('Aadarsh', 1);
    INSERT INTO test VALUES ('Aayush', 2);
    INSERT INTO test VALUES ('Dhruv', 3);

    Output:

    test
    Output

    Now let us try to insert a value with an apostrophe in it.

    INSERT INTO test VALUES ('Aaka'sh', 4);

    The database throws an error:

    Error: near line 12: near "sh": syntax error

    The error does not directly mean it is related to the extra apostrophe in the string but it is caused by that. The database treats the value `’Aaka’sh’` as two separate words including one complete word `’ Aaka ’` and another value starting from `sh’` but not complete yet. Therefore, it points that there is some syntax error near `sh’`.

    How to Insert Values with an Apostrophe

    Let’s understand How to insert a value that contains an apostrophe in SQLite with the below methods.

    Setting Up Environment

    Let us create a table that will have all the string types and insert values with apostrophes into it.

    CREATE TABLE test_dq 
    (
    character_column CHARACTER(20),
    varchar_column VARCHAR(255),
    varying_character_column VARYING CHARACTER(255),
    nchar_column NCHAR(55),
    native_character_column NATIVE CHARACTER(70),
    nvarchar_column NVARCHAR(100),
    text_column TEXT,
    clob_column CLOB
    );

    Method 1: Use Single Quotes Twice to Escape Single Quotes

    When the string is enclosed in single quotes then single quotes have a special meaning to them. They are used to notify the starting and ending of the string. To escape the special meaning we can write the single quote twice consecutively. Let us see the following example.

    INSERT INTO test_dq VALUES 
    (
    '''a''bc''', '''d''ef''', '''g''hi''', '''j''kl''', '''m''no''', '''p''qr''', '''s''tu''', '''v''wx'''
    );

    Output:

    SingleQuotes
    Output

    Explanation : We get the output after inserting into the Use Single Quotes Twice to Escape Single Quotes.

    Method 2: Use Double Quotes to Escape Single Quotes

    When the string is enclosed in double quotes the single quotes behave as character literals rather than having any special meaning attached to them. To escape the single quote we can just enclose the string in double quotes . Let us see the following example.

    INSERT INTO test_dq VALUES 
    (
    "'a'bc'", "'d'ef'", "'g'hi'", "'j'kl'", "'m'no'", "p'qr'", "'s'tu'", "'v'wx'"
    );

    Output:

    DoubleQuotes
    Output

    Explanation : We get the output after inserting into the test table using Double Quotes to Escape Single Quotes.

    Method 3: Use CHAR(39)

    We can use the ASCII value of `’` (39) to insert it into strings. This can be done by concatenating different strings and putting CHAR(39) in between them. In the following the concatenation is done using the `||` operator.

    INSERT INTO test_dq VALUES (CHAR(39) || 'a' || CHAR(39) || 'bc' || CHAR(39), CHAR(39) || 'd' ||
    CHAR(39) || 'ef' || CHAR(39), CHAR(39) || 'g' || CHAR(39) || 'hi' || CHAR(39), CHAR(39) || 'j' || CHAR(39) || 'kl' ||
    CHAR(39), CHAR(39) || 'm' || CHAR(39) || 'no' || CHAR(39), CHAR(39) || 'p' || CHAR(39) || 'qr' || CHAR(39), CHAR(39) || 's' ||
    CHAR(39) || 'tu' || CHAR(39), CHAR(39) || 'v' || CHAR(39) || 'wx' || CHAR(39));

    Output:

    UseCHAR
    Output

    Explanation: We get the output after inserting into the test table using Use CHAR(39).

    Conclusion

    Overall we have seen that we can enclose strings in quotes in SQLite. However to put apostrophes into strings themselves we need to escape them or use some other mechanism. There are three ways of doing this the first being to write the single quotes twice consecutively. The second is to use string literals and the third is to use the CHAR function .

    How to Insert a Value that Contains an Apostrophe in PL/SQL?
    PL/SQL, or Procedural Language/Structured Query Language, is an extension of SQL used for writing procedural code within Oracle databases. It is a proprietary procedural extension of SQL developed by Oracle Corporation specifically for Oracle Database. It combines the power of SQL with procedural constructs like loops, conditions, and exception han
    How to Insert a Value that Contains an Apostrophe in PostgreSQL?
    When working with databases, it's common to encounter situations where values contain special characters like apostrophes. PostgreSQL, a powerful open-source database system, provides several ways to insert values that contain apostrophes safely. In this article, we'll explore different approaches to handle this scenario and ensure data integrity.
    How to Insert a Value that Contains an Apostrophe in SQL?
    SQL is a standard database language used to access and manipulate data in databases. SQL stands for Structured Query Language. SQL was developed by IBM Computer Scientists in the 1970s. By executing queries SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall SQL is a query language that commun
    How to Escape Apostrophe in MySQL?
    Single quotes are used in MySQL to enclose and identify text data. When dealing with text data that contains an apostrophe, it is important to use correct escaping to avoid SQL injection and other security issues. In this article, we will discuss several ways to escape an apostrophe in MySQL, which will make your life easier when dealing with text
    SQLite INSERT INTO SELECT
    SQLite is a lightweight and server-less relational database management system. It requires very minimal configuration which has proven to be very helpful for developers to integrate it into any applications with ease. Due to its server-less architecture, we can use SQLite in various mobile applications as well as in small desktop applications. In t
    How to Insert Multiple Rows in SQLite?
    In the area of database management, efficiency is key. When working with SQLite, a lightweight database engine, inserting multiple rows efficiently can significantly boost performance and speed our workflow. In this article, We will understand how to insert multiple rows in SQLite through various methods along with the examples and so on. How to In
    How to Insert Double and Float Values in SQLite?
    SQLite, a lightweight and server-less relational database management system, provides powerful features for handling various data types, including double and float values. In this article, we will explore various methods along with examples for inserting double and float values into SQLite tables, ensuring data integrity and precision. How to Inser
    How to Insert a Line Break in a SQLite VARCHAR String
    SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserting a line break in an SQLite VARCHAR/NVARCHAR stri
    SQLite – INSERT IF NOT EXISTS ELSE UPDATE
    SQLite is a popular choice for embedded database applications due to its lightweight nature and ease of use. One common scenario in SQLite is the need to insert a row into a table if it does not already exist and update it if it does. In this article, We will explore how to achieve this efficiently by understanding the SQLite's INSERT OR REPLACE st
    How to Set a Column Value to NULL in SQLite?
    SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it into any applications with great ease. In this arti
    How to Select Row With Max Value in in SQLite
    In SQLite, retrieving rows with the maximum value for a specific column, grouped by another column's distinct values can be a challenging task. Whether you're analyzing data trends or identifying top performers, this operation can provide valuable output. In this beginner-friendly guide, we will explore two common methods to fetch such rows using S
    How to fix Cannot Insert Explicit Value For Identity Column in Table in SQL Server
    While working on the SQL Server databases, we need to perform various operations like insert, update, and delete data from the update which are famously known as OLTP operations. In the Insert operation, we developers sometimes face the error message saying - 'Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is
    How to fix Cannot Insert Explicit Value For Identity Column in Table in SQL
    In SQL, the error message "Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF" is a common issue encountered when attempting to insert explicit values into an identity column in a table. This error occurs because SQL, by default, does not allow explicit values to be inserted into identity columns un
    How to Find Document with Array that Contains a Specific Value in MongoDB
    MongoDB is a popular choice for developers working with large volumes of data, due to its flexibility and powerful querying capabilities. One common task developers face is finding documents in a collection where an array field contains a specific value. In this beginner-friendly article, we'll explore simple yet effective methods to achieve this u
    How to Check if an Array Field Contains a Unique Value or Another Array in MongoDB?
    MongoDB is a widely used NoSQL database that enables developers to query complex data structures flexibly. In MongoDB, arrays are commonly used to store lists of values, providing flexibility in data modeling. To ensure data integrity and get valuable insights, it's important to understand how to check for unique values and arrays within array fiel
    SQLite LIKE Operator
    SQLite is a serverless architecture that we use to develop embedded software for devices like televisions, cameras, and so on. It is written in C programming Language. It allows the programs to run without any configuration. In this article, we will learn everything about the LIKE operator. After reading this article you will get decent knowledge a
    SQLite Replace Statement
    SQLite is a database engine. It is a serverless architecture as it does not require any server to process queries. Since it is serverless, it is lightweight and preferable for small datasets. It is used to develop embedded software. It is cross-platform and available for various Operating systems such as Linux, macOS, Windows, Android, and so on. R
    SQLite Transaction
    SQLite is a database engine. It is a software that allows users to interact with relational databases. Basically, it is a serverless database which means it does not require any server to process queries. With the help of SQLite, we can develop embedded software without any configurations. SQLite is preferable for small datasets. SQLite is a portab
    SQLite Create Table
    SQLite is a database engine. It does not require any server to process queries. It is a kind of software library that develops embedded software for television, smartphones, and so on. It can be used as a temporary dataset to get some data within the application due to its efficient nature. It is preferable for small datasets. SQLite CREATE TABLESQ
    SQLite Primary Key
    SQLite is an open-source database system just like SQL database system. It is a lightweight and serverless architecture which means it does not require any server and administrator to run operations and queries. It is widely used by the developer to store the data within the applications. It is preferable for small datasets without much effort. It
    SQLite Foreign Key
    SQLite is a serverless architecture, which does not require any server or administrator to run or process queries. This database system is used to develop embedded software due to its lightweight, and low size. It is used in Desktop applications, mobile applications televisions, and so on. Foreign Key in SQLiteA Foreign Key is a column or set of co
    SQLite Update Statement
    SQLite is a database engine. It is a software that allows users to interact with relational databases, Basically, it is a serverless database which means it does not require any server to process queries. With the help of SQLite, we can develop embedded software without any configurations. SQLite is preferable for small datasets. Update StatementSo
    SQLite WHERE Clause
    SQLite is the serverless database engine that is used most widely. It is written in c programming language and it belongs to the embedded database family. In this article, you will be learning about the where clause and functionality of the where clause in SQLite. Where ClauseSQLite WHERE Clause is used to filter the rows based on the given query.
    SQLite SELECT Query
    SQLite is a serverless, popular, easy-to-use, relational database system that is written in c programming language. SQLite is a database engine that is built into all the popular devices that we use every day of our lives including TV, Mobile, and Computer. As we know that we create the tables in the database and insert the data into them to store
    SQLite ORDER BY Clause
    SQLite is the most popular database engine which is written in C programming language. It is a serverless, easy-to-use relational database system and it is open source and self-contained. In this article, you will gain knowledge on the SQLite ORDER BY clause. By the end of this article, you will get to know how to use the ORDER BY clause, where to
    SQLite UNIQUE Constraint
    SQLite is a lightweight relational database management system (RDBMS). It requires minimal configuration and it is self-contained. It is an embedded database written in C language. It operates a server-less, file-based database engine making it a good fit for mobile applications and simple desktop applications. It supports standard SQL syntax. In t
    SQLite COUNT
    SQLite is a serverless database engine, it is written in C programming language and is easy to use and store the data. It is one of the most used database engines. It is widely used for the development of Embedded applications. In this article we will learn about the SQLite count, how it works and what are all the other functions that are used with
    SQLite Autoincrement
    SQLite is a serverless database engine written in c programming language. It is one of the most used database engines in our everyday life like Mobile Phones, TV, and so on, etc. In this article, we will be learning about autoincrement in SQLite, its functionality, and how it works along with the examples and we will also be covering without autoin
    SQLite Limit clause
    SQLite is the most popular and most used database engine which is written in c programming language. It is serverless and self-contained and used for developing embedded software devices like TVs, Mobile Phones, etc. In this article, we will be learning about the SQLite Limit Clause using examples simply and understandably. How does the SQLite limi
    SQLite CHECK Constraint
    SQLite is a lightweight and embedded Relational Database Management System (commonly known as RDBMS). It is written in C Language. It supports standard SQL syntax. It is a server-less application which means it requires less configuration than any other client-server database (any database that accepts requests from a remote user is known as a clie
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This improvement is locked by another user right now. You can suggest the changes for now and it will be under 'My Suggestions' Tab on Write.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.