开发者

return from sql showing squares

开发者 https://www.devze.com 2023-03-02 14:23 出处:网络
if i use sqlite3.exe the text is returned from my tables correctly however within android i get squares where the spaces are

if i use sqlite3.exe the text is returned from my tables correctly however within android i get squares where the spaces are

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) {

    }

}

RETURNS: sqlite:

return from sql showing squares

logcat and emulator screen:

return from sql showing squares

just wanted to add that the string manipulation being done with .replaceall isnt on the name field at all. and from the output on the screen i can tell its working. PLUS the logcat pic u see was a Log.e placed before all of the manipulation code

also that the db is premade and i get NO errors in logcat when it displays.


i fixed this but forgot the fix. this code does work properly though:

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

    }

}
0

精彩评论

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