我想學習如何爲Android編程數據庫,以及我的知識是SQLite。如果我知道Java我還需要學習什麼,才能構建數據庫?任何其他信息,你可以幫助我將不勝感激。如果我知道Java,我需要知道什麼才能將Android應用程序連接到SQLite數據庫?
-1
A
回答
2
基本上,您需要一個DatabaseHelper類,DataBaseHelper.class的示例:
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
private String sql;
private SQLiteDatabase db;
//database name
private static final String NAME_DB = "Courses.sql";
//database version
private static final int VERSION_DB = 1;
//database table name
public static final String COURSES_TABLE = "courses";
//database table columns for N_TABLE
public static final String ID_KEYROW= "_id";
public static final String ID_COURSE= "courseid";
public static final String ID_TITLE = "title";
public static final String ID_INSTRUCTOR = "instructor";
public static final String ID_LENGTH = "length";
public static final String ID_RATING = "rating";
public static final String ID_TOPIC = "topic";
public static final String ID_SUBJECT = "subject";
public static final String ID_DESCRIPTION = "description";
//private final Context myContext;
public DBHelper(Context context) {
super(context, NAME_DB, null, VERSION_DB);
//this.myContext = context;
}
//Creating Table
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_LOGIN_TABLE = "CREATE TABLE " + COURSES_TABLE + "("
+ ID_KEYROW + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ID_COURSE + " TEXT NOT NULL, "
+ ID_TITLE + " TEXT NOT NULL, "
+ ID_INSTRUCTOR + " TEXT NOT NULL, "
+ ID_LENGTH + " TEXT NOT NULL, "
+ ID_RATING + " TEXT NOT NULL, "
+ ID_TOPIC + " TEXT NOT NULL, "
+ ID_SUBJECT + " TEXT NOT NULL, "
+ ID_DESCRIPTION + " TEXT NOT NULL" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
/* Other Method for Create DataBase.
db.execSQL(" CREATE TABLE courses" + "(" + ID_KEYROW + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ID_COURSE + " TEXT NOT NULL, " + ID_TITLE + " TEXT NOT NULL, " + ID_INSTRUCTOR + " TEXT NOT NULL, " + ID_LENGTH + " TEXT NOT NULL, " + ID_RATING + " TEXT NOT NULL, " + ID_TOPIC + " TEXT NOT NULL, " + ID_SUBJECT + " TEXT NOT NULL, " + ID_DESCRIPTION + " TEXT NOT NULL);"
);*/
}
//Updating Database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + COURSES_TABLE);
onCreate(db);
}
//Insert Courses
//public void insertCourses(Integer courseid, String title, String instructor, Integer length, Integer rating, String topic, String subject, String description)
public void insertCourses()
{
SQLiteDatabase db= this.getWritableDatabase();
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('1', 'Interdisciplinary Care Planning', 'Keith Savell', '60', '4.7', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to...', 'Encourage the Interdisciplinary Treatment Team (IDT) to work as a team and view the Care Plan as a resident-centered collaborative effort to enhance the care and quality of resident life.', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to utilize the Care Plan to truly function as a team - working together to enhance the care and quality of resident life.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('2', 'Culture Change: Creating A Climate Of Care...', 'Keith Savell', '60', '5', 'Culture Change ', 'Learn to evaluate and modify the environment to support our residents with cognitive impairment as they age in place.', 'Residents with dementia and delirium are constantly assessing the environment, looking for clues to help them understand where they are, who others around them are, what they are supposed to be doing and what is expected of them.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('3', 'Medical Records Documentation', 'Keith Savell', '60', '5', 'Medical Records Documentation ', 'Encourage the Interdisciplinary Treatment Team (IDT) to work as a team and view the Care Plan as a resident-centered collaborative effort to enhance the care and quality of resident life.', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to utilize the Care Plan to truly function as a team - working together to enhance the care and quality of resident life.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('4', 'Census Challenges', 'Keith Savell', '60', '4.45', 'Creative Solutions to Drive Census ', 'Learn to evaluate and modify the environment to support our residents with cognitive impairment as they age in place.', 'Residents with dementia and delirium are constantly assessing the environment, looking for clues to help them understand where they are, who others around them are, what they are supposed to be doing and what is expected of them.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('5', 'Fall Prevention: Reducing Fall Related Injuries', 'Keith Savell', '60', '5', 'Guidelines for Health Care Providers ', 'Encourage the Interdisciplinary Treatment Team (IDT) to work as a team and view the Care Plan as a resident-centered collaborative effort to enhance the care and quality of resident life.', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to utilize the Care Plan to truly function as a team - working together to enhance the care and quality of resident life.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('6', 'Meaningful Engagement', 'Keith Savell', '60', '5', 'Creating The Failure Free Activity Program ', 'Learn to evaluate and modify the environment to support our residents with cognitive impairment as they age in place.', 'Residents with dementia and delirium are constantly assessing the environment, looking for clues to help them understand where they are, who others around them are, what they are supposed to be doing and what is expected of them.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('7', 'Dementia and Delirium', 'Keith Savell', '60', '4.69', 'The Importance of Accurate Diagnosis and Treatment ', 'Encourage the Interdisciplinary Treatment Team (IDT) to work as a team and view the Care Plan as a resident-centered collaborative effort to enhance the care and quality of resident life.', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to utilize the Care Plan to truly function as a team - working together to enhance the care and quality of resident life.')";
db.execSQL(sql);
sql="INSERT INTO courses(courseid, title, instructor, length, rating, topic, subject, description) VALUES ('8', 'Delirium', 'Keith Savell', '60', '4.69', 'The Importance of Accurate Diagnosis and Treatment ', 'Encourage the Interdisciplinary Treatment Team (IDT) to work as a team and view the Care Plan as a resident-centered collaborative effort to enhance the care and quality of resident life.', 'Interdisciplinary Care Planning requires members of the Interdisciplinary Treatment Team (IDT) to view the Care Plan as a resident centered collaborative effort - rather than as a staff centered document. Learn how to utilize the Care Plan to truly function as a team - working together to enhance the care and quality of resident life.')";
db.execSQL(sql);
//Other Style for Insert record in DB.
ContentValues values = new ContentValues(); //Here from ContentValues
values.put("courseid", "9");
values.put("title", "xxxxxxx Care Planning");
values.put("instructor", "Keith Savell");
values.put("length", "60");
values.put("rating", "4.7");
values.put("topic", "some topic");
values.put("subject", "some subject");
values.put("description", "some description");
db.insert(COURSES_TABLE, null, values);
}
public ArrayList<Courses> getCourses()
{
db = getWritableDatabase();
sql = "SELECT courseid, title, instructor, length, rating, topic, subject, description FROM courses";
Cursor cursor = db.rawQuery(sql, null);
ArrayList<Courses> courses = new ArrayList<Courses>();
while (cursor.moveToNext())
{
Courses oCourses=new Courses();
oCourses.courseid=cursor.getInt(0);
oCourses.title=cursor.getString(1); //Original Value 0
oCourses.instructor=cursor.getString(2);
oCourses.length=cursor.getString(3);
oCourses.rating=cursor.getString(4);
oCourses.topic=cursor.getString(5);
oCourses.subject=cursor.getString(6);
oCourses.description=cursor.getString(7);
courses.add(oCourses);
}
db.close();
cursor.close();
return courses;
}
public ArrayList<Courses> getItem(int position)
{
db = getWritableDatabase();
sql = "SELECT courseid, title, instructor, length, rating, topic, subject, description FROM courses";
Cursor cursor = db.rawQuery(sql, null);
ArrayList<Courses> courses = new ArrayList<Courses>();
while (cursor.moveToNext())
{
Courses oCourses=new Courses();
oCourses.courseid=cursor.getInt(0);
oCourses.title=cursor.getString(1); //Original Value 0
oCourses.instructor=cursor.getString(2);
oCourses.length=cursor.getString(3);
oCourses.rating=cursor.getString(4);
oCourses.topic=cursor.getString(5);
oCourses.subject=cursor.getString(6);
oCourses.description=cursor.getString(7);
courses.add(oCourses);
}
db.close();
cursor.close();
return courses;
}
public int getRowCount() {
String countQuery = "SELECT * FROM " + COURSES_TABLE;
SQLiteDatabase db =this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
//return row count.
return rowCount;
}//GetRowCount
//Recreate database, delete all tables and create them again.
public void resetTables(){
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(COURSES_TABLE, null, null);
db.close();
}}
後來在您的Mainclass(或活動要管理數據)的DatabaseHelper.class的調用方法,希望這會給你的想法...
2
您需要了解SQL,關係數據庫和規範化規則。
2
首先,您需要在服務器上爲您的SQLite數據庫安裝,命名並建立憑據(用戶名,密碼,主機)。這裏是一個鏈接,涉及Android類需要做到這一點:http://developer.android.com/reference/android/database/sqlite/package-summary.html
然後,你需要了解JAVA將如何與SQL交談。我只熟悉PHP-> SQL,但這裏有一個用於JAVA-> SQL連接的鏈接,它與PHP http://sqlzoo.net/java.htm非常相似。
SQL語言非常簡單。
SELECT
是從數據庫表中獲取行。
INSERT
是插入行的行
UPDATE
是改變值(又稱字段)
DELETE
是刪除行
雖然許多人可能會大便,大便我要這個,我從SQL語法開始(並連接PHP到SQL):W3CSchools
相關問題
- 1. 使用共享數據庫連接時需要知道什麼?
- 2. 我如何知道Android應用程序重新連接到Socket.io?
- 3. 我需要知道什麼才能創建Linux GUI元素
- 4. 我需要什麼知識才能製作數據庫連接的Web應用程序?
- 5. php和newlines:我需要知道什麼?
- 6. 我需要知道從Java到JavaScript?
- 7. 不知道如何將數據庫導入到我的應用程序中android
- 8. 爲了製作使用數據庫的Java應用程序,我需要知道些什麼?
- 9. 如果我們知道android
- 10. Android開發人員轉移到iOS - 我需要知道什麼?
- 11. Python:如何知道我連接到哪個數據庫?
- 12. 想要發佈第一個應用程序我需要知道什麼
- 13. Android應用程序崩潰,我不知道爲什麼
- 14. 如何知道python我需要小數?
- 15. 如何知道我的Android設備上有什麼SQLite數據庫(Galaxy)
- 16. 我需要知道什麼時候應該使用wp_redirect()?
- 17. 在使用IM應用程序之前,我需要知道些什麼?
- 18. 我是否需要知道Emacs Lisp才能使用GNU/Emacs
- 19. 我是否需要知道Python才能使用OpenCV
- 20. 我需要知道數據倉庫需要多少電子表格知識
- 21. 我需要知道如何使用string.maketrans
- 22. 我如何知道用戶要卸載我的應用程序
- 23. 我的應用程序失敗了,我不知道爲什麼
- 24. vsto先決條件我如何知道我需要什麼?
- 25. 如何才能知道什麼是C#
- 26. 的java總之我怎麼能知道是否需要攜帶
- 27. 我的應用程序如何知道使用什麼私鑰
- 28. 在java中我應該知道怎樣才能創建桌面應用程序?
- 29. 如果我知道數據在哪裏,我可以知道什麼數據,例如0xffffffff7fffd9d8與0x10019c1e0?
- 30. 知道什麼應用程序調用我的遠程服務