2012-12-17 76 views
0

好,其他的崗位,我的意思是:如何組織類,同時使數據庫應用程序使用SQLite

Should there be one SQLiteOpenHelper for each table in the database?

我是懷疑具有一種或應用更加助手,好了,anwser是1個幫手,很多專欄。

現在的第二部分,我其實做這個教程: http://www.vogella.com/articles/AndroidSQLite/article.html

,但我想通過在數據庫上超過1臺辦呢,好吧,我所做的工作,現在我嘗試創建所謂的數據訪問對象「DAO」。問題是一樣的,最好是有一個單獨的DAO,或者最好每個表都有一個(每個表類都有一個),最後是一個單獨的DAO類,其中包含所有可用於「操作」的DAO類應用程序...?

這是在本教程所謂DAO:

package de.vogella.android.sqlite.first; 

import java.util.ArrayList; 
import java.util.List; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

public class CommentsDataSource { 

    // Database fields 
    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
     MySQLiteHelper.COLUMN_COMMENT }; 

    public CommentsDataSource(Context context) { 
    dbHelper = new MySQLiteHelper(context); 
    } 

    public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
    dbHelper.close(); 
    } 

    public Comment createComment(String comment) { 
    ContentValues values = new ContentValues(); 
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment); 
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, 
     values); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
     allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, 
     null, null, null); 
    cursor.moveToFirst(); 
    Comment newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
    } 

    public void deleteComment(Comment comment) { 
    long id = comment.getId(); 
    System.out.println("Comment deleted with id: " + id); 
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID 
     + " = " + id, null); 
    } 

    public List<Comment> getAllComments() { 
    List<Comment> comments = new ArrayList<Comment>(); 

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
     allColumns, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     Comment comment = cursorToComment(cursor); 
     comments.add(comment); 
     cursor.moveToNext(); 
    } 
    // Make sure to close the cursor 
    cursor.close(); 
    return comments; 
    } 

    private Comment cursorToComment(Cursor cursor) { 
    Comment comment = new Comment(); 
    comment.setId(cursor.getLong(0)); 
    comment.setComment(cursor.getString(1)); 
    return comment; 
    } 
} 

在此先感謝。

回答

1

這是幫助您的應用程序與數據庫進行交互的類。你應該只有一個DAO類和你需要的所有方法。 (不同的方法對不同的表。)

檢查下面的例子:

public void insertInTableA (String[]) //or any args 
{ 
    //Logic for table A row insertion 
} 

public void insertInTableB (String[]) //or any args 
{ 
    //Logic for table B row insertion 
} 

public void dltFromTableA (String where) //or any args 
{ 
    //Logic for table A row deletion 
} 

public void dltFromTableB (String where) //or any args 
{ 
    //Logic for table B row deletion 
} 

//Other function as per requirement 
+1

謝謝,你真是幫了我很多。 –

相關問題