Restriciting Number of Characters entered into SQLite3











up vote
1
down vote

favorite
1












I'm trying to create an SQL database with the following fields:



connection= sqlite3.connect('Main Database')
crsr = connection.cursor()
#Creates a table for the teacher data if no table is found on the system
crsr.execute("""CREATE TABLE IF NOT EXISTS Teacher_Table(Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL,
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL);""")
connection.commit()
connection.close()


But when I input values, the gender field accepts more than one value
Database View



How can I make sure it only accepts one character for that field










share|improve this question







New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    SqlLite ignores the length constraints sqlite.org/datatype3.html
    – Willem Van Onsem
    2 days ago










  • What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
    – varro
    2 days ago















up vote
1
down vote

favorite
1












I'm trying to create an SQL database with the following fields:



connection= sqlite3.connect('Main Database')
crsr = connection.cursor()
#Creates a table for the teacher data if no table is found on the system
crsr.execute("""CREATE TABLE IF NOT EXISTS Teacher_Table(Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL,
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL);""")
connection.commit()
connection.close()


But when I input values, the gender field accepts more than one value
Database View



How can I make sure it only accepts one character for that field










share|improve this question







New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    SqlLite ignores the length constraints sqlite.org/datatype3.html
    – Willem Van Onsem
    2 days ago










  • What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
    – varro
    2 days ago













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I'm trying to create an SQL database with the following fields:



connection= sqlite3.connect('Main Database')
crsr = connection.cursor()
#Creates a table for the teacher data if no table is found on the system
crsr.execute("""CREATE TABLE IF NOT EXISTS Teacher_Table(Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL,
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL);""")
connection.commit()
connection.close()


But when I input values, the gender field accepts more than one value
Database View



How can I make sure it only accepts one character for that field










share|improve this question







New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm trying to create an SQL database with the following fields:



connection= sqlite3.connect('Main Database')
crsr = connection.cursor()
#Creates a table for the teacher data if no table is found on the system
crsr.execute("""CREATE TABLE IF NOT EXISTS Teacher_Table(Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL,
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL);""")
connection.commit()
connection.close()


But when I input values, the gender field accepts more than one value
Database View



How can I make sure it only accepts one character for that field







python tkinter sqlite3






share|improve this question







New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Osasenaga Emokpae

61




61




New contributor




Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Osasenaga Emokpae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    SqlLite ignores the length constraints sqlite.org/datatype3.html
    – Willem Van Onsem
    2 days ago










  • What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
    – varro
    2 days ago














  • 1




    SqlLite ignores the length constraints sqlite.org/datatype3.html
    – Willem Van Onsem
    2 days ago










  • What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
    – varro
    2 days ago








1




1




SqlLite ignores the length constraints sqlite.org/datatype3.html
– Willem Van Onsem
2 days ago




SqlLite ignores the length constraints sqlite.org/datatype3.html
– Willem Van Onsem
2 days ago












What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
– varro
2 days ago




What do you want to happen if an attempt is made to insert more than one character: get an error or silently truncate to one character?
– varro
2 days ago












1 Answer
1






active

oldest

votes

















up vote
2
down vote














How can I make sure it only accepts one character for that field




SQLite does not check the length constraints defined at type level, as is specified in the documentation on types:




(...) 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.




So you can not enforce this at the database level. You will thus need to enforce this through your views, etc.



We can however, like @Ilja Everilä says, use a CHECK constraint:



CREATE TABLE IF NOT EXISTS Teacher_Table(
Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL CHECK (length(Gender) < 2),
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL
)





share|improve this answer























  • Would a CHECK constraint work in SQLite?
    – Ilja Everilä
    2 days ago










  • @IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
    – Willem Van Onsem
    2 days ago






  • 1




    Thank you so much! This worked for me
    – Osasenaga Emokpae
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Osasenaga Emokpae is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239175%2frestriciting-number-of-characters-entered-into-sqlite3%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote














How can I make sure it only accepts one character for that field




SQLite does not check the length constraints defined at type level, as is specified in the documentation on types:




(...) 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.




So you can not enforce this at the database level. You will thus need to enforce this through your views, etc.



We can however, like @Ilja Everilä says, use a CHECK constraint:



CREATE TABLE IF NOT EXISTS Teacher_Table(
Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL CHECK (length(Gender) < 2),
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL
)





share|improve this answer























  • Would a CHECK constraint work in SQLite?
    – Ilja Everilä
    2 days ago










  • @IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
    – Willem Van Onsem
    2 days ago






  • 1




    Thank you so much! This worked for me
    – Osasenaga Emokpae
    2 days ago















up vote
2
down vote














How can I make sure it only accepts one character for that field




SQLite does not check the length constraints defined at type level, as is specified in the documentation on types:




(...) 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.




So you can not enforce this at the database level. You will thus need to enforce this through your views, etc.



We can however, like @Ilja Everilä says, use a CHECK constraint:



CREATE TABLE IF NOT EXISTS Teacher_Table(
Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL CHECK (length(Gender) < 2),
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL
)





share|improve this answer























  • Would a CHECK constraint work in SQLite?
    – Ilja Everilä
    2 days ago










  • @IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
    – Willem Van Onsem
    2 days ago






  • 1




    Thank you so much! This worked for me
    – Osasenaga Emokpae
    2 days ago













up vote
2
down vote










up vote
2
down vote










How can I make sure it only accepts one character for that field




SQLite does not check the length constraints defined at type level, as is specified in the documentation on types:




(...) 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.




So you can not enforce this at the database level. You will thus need to enforce this through your views, etc.



We can however, like @Ilja Everilä says, use a CHECK constraint:



CREATE TABLE IF NOT EXISTS Teacher_Table(
Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL CHECK (length(Gender) < 2),
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL
)





share|improve this answer















How can I make sure it only accepts one character for that field




SQLite does not check the length constraints defined at type level, as is specified in the documentation on types:




(...) 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.




So you can not enforce this at the database level. You will thus need to enforce this through your views, etc.



We can however, like @Ilja Everilä says, use a CHECK constraint:



CREATE TABLE IF NOT EXISTS Teacher_Table(
Teacher_ID INTEGER PRIMARY KEY,
TFirst_Name VARCHAR(25) NOT NULL,
TLast_Name VARCHAR (25) NOT NULL,
Gender CHAR(1) NOT NULL CHECK (length(Gender) < 2),
Home_Address VARCHAR (50) NOT NULL,
Contact_Number VARCHAR (14) NOT NULL
)






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Willem Van Onsem

139k16131221




139k16131221












  • Would a CHECK constraint work in SQLite?
    – Ilja Everilä
    2 days ago










  • @IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
    – Willem Van Onsem
    2 days ago






  • 1




    Thank you so much! This worked for me
    – Osasenaga Emokpae
    2 days ago


















  • Would a CHECK constraint work in SQLite?
    – Ilja Everilä
    2 days ago










  • @IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
    – Willem Van Onsem
    2 days ago






  • 1




    Thank you so much! This worked for me
    – Osasenaga Emokpae
    2 days ago
















Would a CHECK constraint work in SQLite?
– Ilja Everilä
2 days ago




Would a CHECK constraint work in SQLite?
– Ilja Everilä
2 days ago












@IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
– Willem Van Onsem
2 days ago




@IljaEverilä: yes, funny enough, MySQL ignores CHECK, making the "SQL landscape" quite chaotic :s.
– Willem Van Onsem
2 days ago




1




1




Thank you so much! This worked for me
– Osasenaga Emokpae
2 days ago




Thank you so much! This worked for me
– Osasenaga Emokpae
2 days ago










Osasenaga Emokpae is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Osasenaga Emokpae is a new contributor. Be nice, and check out our Code of Conduct.













Osasenaga Emokpae is a new contributor. Be nice, and check out our Code of Conduct.












Osasenaga Emokpae is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239175%2frestriciting-number-of-characters-entered-into-sqlite3%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python