开发者

Example of how to implement ALTER TABLE [duplicate]

开发者 https://www.devze.com 2023-03-18 04:57 出处:网络
This question already has answers here: adding a column to SQLite table in Android? (3 answers) Closed 8 years ago.
This question already has answers here: adding a column to SQLite table in Android? (3 answers) Closed 8 years ago.

开发者_C百科Ive asked this question several times and yet to get a COMPLETE answer... How to you implement the ALTER TABLE statement to add a column to a database. Can someone PLEASE give me an example?


Please read the SQLite ALTER TABLE reference, and this Android SQLite article:

database.execSQL("ALTER TABLE " + your_table_name + " ADD COLUMN " + new_col_name + " int");


ALTER TABLE db_name.mytable ADD COLUMN column_name VARCHAR(30)

That's for SQLite. Other databases may allow you to specify where the column gets put, but with SQLite it's appended to the end.

EDIT:I thought about this some more. In your question you asked for a complete answer. And someone identified that you asked essentially the same question just 5 hours before asking this one. Considering that none of the responses in that previous thread were what you were looking for (despite some of them offering complete SQL examples), it strikes me that a complete answer must include something more than the SQL and a few links to web tutorials.

In an effort to be thorough and complete, let me suggest that you consider the following books: Using SQLite (O'Reilly), and Learning Android (O'Reilly). In fact, the latter has a chapter (chapter 9) devoted to the Android database environment, meaning Android's implementation of SQLite.

People who have a lot of experience with databases, with programming, and with diverse operating systems often can get by with web manuals and a few tips on sites such as StackOverflow. But for the rest of us (myself included), when I really want to learn a topic that I consider important to me, I immerse myself in a couple of good books along the way.

I hope that you, or someone else who may be just getting started with such topics, find this advice helpful.


I've just encountered this and tried over and over and got the exception described here about column not exists, despite the alter table statement didn't throw any exception.

After two hours of trying, setting up a file backup storage, and whatever seemed viable here, I realized that I didn't include the new column in the "select query", but still tried to extract the data from the cursor, hence the column does not exist exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消