从显示正方形的sql返回

时间:2011-05-02 20:40:47

标签: android

如果我使用sqlite3.exe,则正确地从我的表中返回文本 但是在android中我得到空格

的正方形
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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;
import android.util.Log;
import android.widget.TextView;

public class SqlLiteHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "movelist";

    private static String DB_PATH = "/data/data/ packagename /databases/";

    private Context context;

    private SQLiteDatabase DB;

    public SqlLiteHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.context = context;
    }

    public List<TextView> getTextViews(String charName, int consoleSwitch,
            int textSize, String moveTypeSource) {

        List<TextView> moveList = new ArrayList<TextView>();
        String movesSql = new String();
        String tableName = new String();

        if (moveTypeSource.equals("Grappling")) {
            tableName = "SevenTypedMoves";
            movesSql = "SELECT moveCommand, moveName, moveType, moveDmg, moveEscape, moveProperties FROM "
                    + tableName
                    + " WHERE character_name = '"
                    + charName
                    + "' and moveArt = '" + moveTypeSource + "'";
        }
        DB = getWritableDatabase();

        Cursor movesCursor = DB.rawQuery(movesSql, null);

        if (movesCursor.moveToFirst()) {
            do {
                String moveCommand = movesCursor.getString(movesCursor
                        .getColumnIndex("moveCommand"));
                String moveName = movesCursor.getString(movesCursor
                        .getColumnIndex("moveName"));
                String moveType = movesCursor.getString(movesCursor
                        .getColumnIndex("moveType"));
                String moveDmg = movesCursor.getString(movesCursor
                        .getColumnIndex("moveDmg"));
                String moveEscape = movesCursor.getString(movesCursor
                        .getColumnIndex("moveEscape"));
                String moveProperties = movesCursor.getString(movesCursor
                        .getColumnIndex("moveProperties"));
                Log.e("movename: ", moveName);

                if (consoleSwitch == 1) {
                    // PS3
                    moveCommand = moveCommand.replaceAll("1", "(͹)");
                    moveCommand = moveCommand.replaceAll("2", "(▲)");
                    moveCommand = moveCommand.replaceAll("3", "(X)");
                    moveCommand = moveCommand.replaceAll("4", "(O)");
                    moveEscape = moveEscape.replaceAll("1", "(͹)");
                    moveEscape = moveEscape.replaceAll("2", "(▲)");
                    moveEscape = moveEscape.replaceAll("3", "(X)");
                    moveEscape = moveEscape.replaceAll("4", "(O)");
                } else if (consoleSwitch == 2) {
                    // XBOX
                    moveCommand = moveCommand.replaceAll("1", "(X)");
                    moveCommand = moveCommand.replaceAll("2", "(Y)");
                    moveCommand = moveCommand.replaceAll("3", "(A)");
                    moveCommand = moveCommand.replaceAll("4", "(B)");
                    moveEscape = moveEscape.replaceAll("1", "(X)");
                    moveEscape = moveEscape.replaceAll("2", "(Y)");
                    moveEscape = moveEscape.replaceAll("3", "(A)");
                    moveEscape = moveEscape.replaceAll("4", "(B)");
                }

                String s = "|" + moveName + ": " + moveCommand
                        + " |Positioning: " + moveType + " |Escape: "
                        + moveEscape + " |Properties: " + moveProperties;

                TextView Move = new TextView(context);
                Move.setText(s);
                if (textSize == 1) {
                    Move.setTextSize(20);
                } else {
                }
                moveList.add(Move);
            } while (movesCursor.moveToNext());
        }
        movesCursor.close();

        String footnoteSql = "SELECT footnoteText FROM footnote WHERE charname = '"
                + charName + "' and movesType = '" + moveTypeSource + "'";
        Cursor footnoteCursor = DB.rawQuery(footnoteSql, null);
        if (footnoteCursor.moveToFirst()) {
            do {
                TextView footnoteMove = new TextView(context);
                String footnoteBodyText = footnoteCursor
                        .getString(footnoteCursor
                                .getColumnIndex("footnoteText"));
                String footnoteText = String.format("FOOTNOTES: \n%s",
                        footnoteBodyText);
                footnoteMove.setText(footnoteText);
                moveList.add(footnoteMove);
            } while (footnoteCursor.moveToNext());
        }
        footnoteCursor.close();

        DB.close();
        return moveList;
    }

    public List<String> getTypes() {
        List<String> types = new ArrayList<String>();
        String footnoteSql = "SELECT footnoteText FROM footnote WHERE charname = '"
                + charName + "' and movesType = '" + moveTypeSource + "'";
        Cursor footnoteCursor = DB.rawQuery(footnoteSql, null);
        return types;
    }

    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            // do nothing - database already exist
        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e("Error copying database: ", e.toString());
            }
        }
    }

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

            // database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {

        InputStream myInput = context.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        DB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

    @Override
    public synchronized void close() {

        if (DB != null)
            DB.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

返回值: 源码:SQLITE RETURN

logcat和模拟器屏幕:logcatreturns

只是想补充说,使用.replaceall完成的字符串操作根本不在名称字段上。从屏幕上的输出我可以告诉它的工作。加上logcat pic你看到的是一个放在所有操作代码之前的Log.e

此外,db是预制的,当它显示时我在logcat中得到NO错误。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,但忘了修复。这段代码可以正常工作:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;

public class SqlLiteHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "tekkenmovelist";

    private static String DB_PATH = "/<path deleted>/";

    private Context context;

    private SQLiteDatabase DB;

    public SqlLiteHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.context = context;
    }

    public List<TextView> getTextViews(String charName, int consoleSwitch,
            int textSize, String moveCatagory, int filterEscape, int filterDmg,
            String catMask) {

        List<TextView> moveList = new ArrayList<TextView>();
        String movesSql = new String();

        movesSql = "SELECT * FROM Moves" + " WHERE charName = '" + charName
                + "' and moveCatagory = '" + moveCatagory + "'";

        DB = getWritableDatabase();

        String textviewText = new String();
        Cursor movesCursor = DB.rawQuery(movesSql, null);

        if (movesCursor.moveToFirst()) {
            do {
                String moveId = movesCursor.getString(movesCursor
                        .getColumnIndex("_id"));

                String moveCommand = new String("");
                if (!movesCursor.isNull(movesCursor.getColumnIndex("moveCommand"))){

                moveCommand = movesCursor.getString(movesCursor
                        .getColumnIndex("moveCommand"));
                }


                String moveName = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveName"))) {
                moveName = movesCursor.getString(movesCursor
                        .getColumnIndex("moveName"));
                }

                String moveType = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveType"))) {
                moveType = movesCursor.getString(movesCursor
                        .getColumnIndex("moveType"));
                }

                String moveDmg = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveDmg"))) {
                moveDmg = movesCursor.getString(movesCursor
                        .getColumnIndex("moveDmg"));
                }

                String moveEscape = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveEscape"))) {
                moveEscape = movesCursor.getString(movesCursor
                        .getColumnIndex("moveEscape"));
                }

                String moveProperties = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveProperties"))) {
                moveProperties = movesCursor.getString(movesCursor
                        .getColumnIndex("moveProperties"));
                }

                String moveStance = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveStance"))) {
                    moveStance = movesCursor.getString(movesCursor
                            .getColumnIndex("moveStance"));
                }

                String moveHitRange = new String("");
                if (!movesCursor.isNull(movesCursor
                        .getColumnIndex("moveHitRange"))) {
                    moveHitRange = movesCursor.getString(movesCursor
                            .getColumnIndex("moveHitRange"));
                }

                String moveHits = new String("");
                if (!movesCursor.isNull(movesCursor.getColumnIndex("moveHits"))) {
                    moveHits = movesCursor.getString(movesCursor
                            .getColumnIndex("moveHits"));
                }

                if (consoleSwitch == 1) {
                    // PS3
                    moveCommand = moveCommand.replaceAll("1", "(͹)");
                    moveCommand = moveCommand.replaceAll("2", "(Δ)");
                    moveCommand = moveCommand.replaceAll("3", "(X)");
                    moveCommand = moveCommand.replaceAll("4", "(O)");
                    moveEscape = moveEscape.replaceAll("1", "(͹)");
                    moveEscape = moveEscape.replaceAll("2", "(Δ)");
                    moveEscape = moveEscape.replaceAll("3", "(X)");
                    moveEscape = moveEscape.replaceAll("4", "(O)");

                } else if (consoleSwitch == 0) {
                    // XBOX
                    moveCommand = moveCommand.replaceAll("1", "(X)");
                    moveCommand = moveCommand.replaceAll("2", "(Y)");
                    moveCommand = moveCommand.replaceAll("3", "(A)");
                    moveCommand = moveCommand.replaceAll("4", "(B)");
                    moveEscape = moveEscape.replaceAll("1", "(X)");
                    moveEscape = moveEscape.replaceAll("2", "(Y)");
                    moveEscape = moveEscape.replaceAll("3", "(A)");
                    moveEscape = moveEscape.replaceAll("4", "(B)");
                }

                    if (!moveCommand.equals("")) {
                        textviewText = "|" + moveCommand;
                    }
                    if (!moveName.equals("")) {
                        textviewText = textviewText + " |Name: " + moveName;
                    }
                    if (!moveHits.equals("")) {
                        textviewText = textviewText + " |Hits: " + moveHits;
                    }
                    if (!moveType.equals("")) {
                        textviewText = textviewText + " |Type: " + moveType;
                    }
                    if (!moveStance.equals("")) {
                        textviewText = textviewText + " |Stance: " + moveStance;
                    }
                    if (!moveDmg.equals("") && filterDmg != 1) {
                        textviewText = textviewText + " |Dmg: " + moveDmg;
                    }
                    if (!moveHitRange.equals("")) {
                        textviewText = textviewText + " |Hit Range: "+ moveHitRange;
                    }
                    if (!moveEscape.equals("") && !moveEscape.equals("None")
                            && filterEscape != 1) {
                        textviewText = textviewText + " |Escape: " + moveEscape;
                    }
                    if (!moveProperties.equals("")) {
                        textviewText = textviewText + " |Properties: "+ moveProperties;
                    }

                TextView Move = new TextView(context);
                Move.setText(textviewText);
                textviewText="";
                if (textSize == 1) {
                    Move.setTextSize(20);
                }
                moveList.add(Move);
            } while (movesCursor.moveToNext());
        }
        movesCursor.close();

        String footnoteSql = "SELECT footnoteText FROM footnotes WHERE charname = '"
                + charName + "' and moveCatagory = '" + moveCatagory + "'";
        Cursor footnoteCursor = DB.rawQuery(footnoteSql, null);
        if (footnoteCursor.moveToFirst()) {
            do {
                TextView footnoteMove = new TextView(context);
                String footnoteBodyText = footnoteCursor
                        .getString(footnoteCursor
                                .getColumnIndex("footnoteText"));
                String footnoteText = String.format("\nFOOTNOTES: \n%s",
                        footnoteBodyText);
                footnoteMove.setText(footnoteText);
                moveList.add(footnoteMove);
            } while (footnoteCursor.moveToNext());
        }
        footnoteCursor.close();

        DB.close();
        return moveList;
    }



    public List<String> getCatagories(String charName) {
        DB = getWritableDatabase();
        List<String> catagories = new ArrayList<String>();
        String catagory = "SELECT moveCatagory FROM characters WHERE charname = '"
                + charName + "'";
        Cursor catagoriesCursor = DB.rawQuery(catagory, null);
        if (catagoriesCursor.moveToFirst()) {
            do {
                String currentCatagory = catagoriesCursor
                        .getString(catagoriesCursor
                                .getColumnIndex("moveCatagory"));
                catagories.add(currentCatagory);
            } while (catagoriesCursor.moveToNext());
        }
        catagoriesCursor.close();
        DB.close();
        return catagories;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    public boolean isDataBaseExist() {
        File dbFile = new File(DB_PATH+DB_NAME);
        return dbFile.exists();
        }
    public void copyDataBase() throws IOException {
        InputStream myInput = context.getAssets().open(DB_NAME);

        File f = new File( DB_PATH );
        if ( !f.exists() )
            f.mkdir();

        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
                        myOutput.write(buffer, 0, length);
        }
                myOutput.flush();
                myOutput.close();
                myInput.close();
        }




    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        DB = getWritableDatabase();
        File f = new File( DB_PATH+DB_NAME );
        if ( !f.exists() )
            f.mkdir();
        f.delete();
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DB.close();

    }

}