Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of characters of a text field

Tags:

sqlite

I want a field "name" be long at most 20 characters...is it possible in sqllite?

like image 890
user1610075 Avatar asked Aug 21 '12 14:08

user1610075


People also ask

How do I limit characters in a text field?

Right-click the text box for which you want to limit characters, and then click Text Box Properties on the shortcut menu. Click the Display tab. Under Options, select the Limit text box to check box, and then specify the number of characters that you want.

Which attribute is set to limit the number of characters in a text field?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.

What are the limits of the text field size in HTML?

The maxlength attribute specifies the maximum number of characters that can be entered. By default, the maximum is 524,288 characters.

How do I restrict text area in HTML?

To set the width and height of the textarea of your own choice, you can specify the desired number of rows and columns. For this, we can make use of rows and cols attributes of <textarea> tag. Following is the code shown below along with the output.


2 Answers

Yes with CHECK CONSTRAINTS. Here is an example enforcing TEXT datatype with a length of less than or equal to 20 characters.

CREATE TABLE IF NOT EXISTS "test"
(
    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "name" TEXT NOT NULL
    CHECK(
        typeof("name") = "text" AND
        length("name") <= 20
    )
);

INSERT INTO "test" ("name") VALUES ("longer than twenty characters");

Result:

Error: CHECK constraint failed: test

Probably too late to help the OP but maybe someone else will find this useful.

like image 98
Martin Hurford Avatar answered Oct 22 '22 01:10

Martin Hurford


No. Per Datatypes In SQLite Version 3,

Note that numeric arguments in parentheses that following the type name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not impose any length restrictions (other than the large global SQLITE_MAX_LENGTH limit) on the length of strings, BLOBs or numeric values.

like image 33
Dave Sherohman Avatar answered Oct 22 '22 00:10

Dave Sherohman