在React.js中,如何从组件的字符串表示中呈现组件?

时间:2015-07-24 19:51:25

标签: javascript reactjs components render

我的问题特别是我希望能够创建动态模板,具体取决于某个记录中包含的项目类型。我想将这些组件作为字符串存储在数据库中,并在加载项时动态呈现它们。模板需要能够处理动态反应变量。它们不仅仅是html字符串,就像stackoverflow中的一些其他示例一样。

怎么可能这样做呢?它甚至可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用 Error on log cat reads android.database.sqlite.SQLiteException: near "TABLEUSER": syntax error (code 1): , while compiling: CREATE TABLEUSER(COLUMNUIDINTEGER PRIMARY KEY AUTOINCREMENT,COLUMN_USERNAMETEXT, COLUMN_EMAILTEXT,FOREIGN KEY(COLUMNUID) REFERENCES TABLESESSIONS(COLUMNSESSIONUID)) Class is like this package databasepackage; /** * Importing the context functionality */ import android.content.ContentValues; import android.content.Context; /** * Importing functionality of SQLite database */ import android.database.sqlite.SQLiteDatabase; /** * Importing functionality of cursor */ //import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * Importing the SQlite open helper class */ import android.database.sqlite.SQLiteOpenHelper; /** * Importing the functionality to allow a log to see if database has been changed/upgraded */ import android.util.Log; /** * class to allow database to access tables * * @author James * */ public class DatabaseHelper extends SQLiteOpenHelper { /** * Tag to allow us to see when database has been upgraded */ public static final String TAG = "DatabaseHelper"; /** * columns of table user * */ public static final String TABLEUSER = "USER"; public static final String COLUMNUID = "COLUMNUID"; public static final String COLUMNUSERNAME = "COLUMN_USERNAME"; public static final String COLUMNEMAIL="COLUMN_EMAIL"; /** * columns of table sessions */ public static final String TABLESESSIONS = "sessions"; public static final String COLUMNSID = "sid"; public static final String COLUMNSESSIONUID = "sessionuid"; public static final String COLUMNSESSIONDATE = "sessiondate"; /** * columns of table session questions */ public static final String TABLESESSIONQUESTION = "sessionquestion"; public static final String COLUMNSESSIONQUESTIONQID = "sessionquestionqid"; public static final String COLUMNSESSIONQUESTIONSID = "sessionquestionsid"; /** * columns of table question */ public static final String TABLEQUESTION = "question"; public static final String COLUMNQUESTIONQID = "questionqid"; public static final String COLUMNQUESTIONSID = "sessionid"; public static final String COLUMNQTEXT = "qtext"; /** * columns of table text entry */ public static final String TABLETEXTENTRY = "textentry"; public static final String COLUMNTEXTENTRYQID = "textentryqid"; public static final String COLUMNTEXTENTRYSID = "textentrysid"; public static final String COLUMNTEXTENTRYQTEXT = "textentryqtext"; public static final String COLUMNBLANKOPTION = "blankoption"; /** * columns of table multichoice */ public static final String TABLEMULTICHOICE = "multichoice"; public static final String COLUMNMULTICHOICEQID = "multichoiceqid"; public static final String COLUMNMULTICHOICESID = "multichoicesid"; public static final String COLUMNMULTICHOICEQTEXT = "multichoiceqtext"; public static final String COLUMNMULTICHOICEOPTA = "multichoiceopta"; public static final String COLUMNMULTICHOICEOPTB = "multichoiceoptb"; public static final String COLUMNMULTICHOICEOPTC = "multichoiceoptc"; public static final String COLUMNMULTICHOICEOPTD = "multichoiceoptd"; /** * columns of table answer */ public static final String TABLEANSWER = "answer"; public static final String COLUMNAID = "aid"; public static final String COLUMNANSWERQID = "answerqid"; public static final String COLUMNANSWERTEXT = "answertext"; public static final String COLUMNISCORRECT = "iscorrect"; /** * creating the database name */ public static final String DATABASE_NAME = "project.db"; /** * setting the database version */ public static final int DATABASE_VERSION = 34; /** * SQL statement of the user table creation * */ public static final String SQLCREATETABLEUSER = "CREATE TABLE" + TABLEUSER + "(" + COLUMNUID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMNUSERNAME + "TEXT," + COLUMNEMAIL + "TEXT," + "FOREIGN KEY(COLUMNUID) REFERENCES TABLESESSIONS(COLUMNSESSIONUID)" + ")"; /** * SQL statment of the sessions table creation */ public static final String SQLCREATETABLESESSIONS = "CREATE TABLE" + TABLESESSIONS + "(" + COLUMNSID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMNSESSIONUID + "INTEGER PRIMARY KEY," + COLUMNSESSIONDATE + "REAL," + "FOREIGN KEY (COLUMNSID) REFERENCES TABLESESSIONQUESTION(COLUMNSESSIONQUESTIONSID)" + ")"; /** * SQL statement of the sessionquestion table */ public static final String SQLCREATETABLESESSIONQUESTION = "CREATE TABLE" + TABLESESSIONQUESTION + "(" + COLUMNSESSIONQUESTIONQID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMNSESSIONQUESTIONSID + "INTEGER NOT NULL," + "FOREIGN KEY (COLUMNSESSIONQUESTIONQID) REFERENCES TABLEQUESTION (COLUMNQUESTIONQID)" + ")"; /** * SQL statement of the question table */ public static final String SQLCREATETABLEQUESTION = "CREATE TABLE" + TABLEQUESTION + "(" + COLUMNQUESTIONQID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMNQUESTIONSID + "INTEGER PRIMARY KEY," + COLUMNQTEXT + "TEXT NOT NULL," + "FOREIGN KEY(COLUMNQUESTIONQID) REFERENCES TABLETEXTENTRY (COLUMNTEXTENTRYQID)," + "FOREIGN_KEY(COLUMNQUESTIONSID) REFERENCES TABLE_MULTICHOICE (COLUMNMULTICHOICESID)" + ")"; /** * SQL statement of the text entry table */ public static final String SQLCREATETABLETEXTENTRY = "CREATE TABLE" + TABLETEXTENTRY + "(" + COLUMNTEXTENTRYQID + "INTEGER PRIMARY KEY," + COLUMNTEXTENTRYSID + "INTEGER PRIMARY KEY," + COLUMNTEXTENTRYQTEXT + "TEXT NOT NULL," + COLUMNBLANKOPTION + "TEXT NOT NULL," + "FOREIGN KEY(COLUMNTEXTENTRYQID) REFERENCES TABLE_ANSWER (COLUMNANSWERQID)" + ")"; /** * SQL statement of the multichoice table */ public static final String SQLCREATETABLEMULTICHOICE = "CREATE TABLE" + TABLEMULTICHOICE + "(" + COLUMNMULTICHOICEQID + "INTEGER PRIMARY KEY," + COLUMNMULTICHOICESID + "INTEGER PRIMARY KEY," + COLUMNMULTICHOICEQTEXT + "TEXT NOT NULL," + COLUMNMULTICHOICEOPTA + "TEXT NOT NULL," + COLUMNMULTICHOICEOPTB + "TEXT NOT NULL," + COLUMNMULTICHOICEOPTC + "TEXT NOT NULL," + COLUMNMULTICHOICEOPTD + "TEXT NOT NULL," + "FOREIGN KEY(COLUMNMULTICHOICEQID) REFERENCES TABLEANSWER (COLUMNANSWERQID)" + ")"; /** * SQL statement of the answer table */ public static final String SQLCREATETABLEANSWER = "CREATE TABLE" + TABLEANSWER + "(" + COLUMNAID + "INTEGER PRIMARY KEY," + COLUMNANSWERQID + "INTEGER PRIMARY KEY," + COLUMNANSWERTEXT + "TEXT NOT NULL," + COLUMNISCORRECT + "INTEGER" + ")"; /** * constructor of the database helper class * * @param context */ public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override /** * creating/executing the tables using the SQL statement */ public void onCreate(SQLiteDatabase db) { db.execSQL(SQLCREATETABLEUSER); db.execSQL(SQLCREATETABLESESSIONS); db.execSQL(SQLCREATETABLESESSIONQUESTION); db.execSQL(SQLCREATETABLEQUESTION); db.execSQL(SQLCREATETABLETEXTENTRY); db.execSQL(SQLCREATETABLEMULTICHOICE); db.execSQL(SQLCREATETABLEANSWER); } /** * Method to allow us to upgrade the table */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "UPGRADING THE DATABSE FROM VERSION" + oldVersion + " to" + newVersion); //db.execSQL("ALTER TABLE" + TABLE_USER + DATABASE_NAME + " ADD COLUMN_EMAIL TEXT"); // clear all data //db.execSQL("DROP TABLE IF EXISTS" + TABLE_USER); db.execSQL("DROP TABLE IF EXISTS" + TABLESESSIONS); db.execSQL("DROP TABLE IF EXISTS" + TABLESESSIONQUESTION); db.execSQL("DROP TABLE IF EXISTS" + TABLEQUESTION); db.execSQL("DROP TABLE IF EXISTS" + TABLETEXTENTRY); db.execSQL("DROP TABLE IF EXISTS" + TABLEMULTICHOICE); db.execSQL("DROP TABLE IF EXISTS" + TABLEANSWER); // recreate the tables onCreate(db); }/** Method to allow users to insert data into users table * **/ public boolean insertData(String username,String email) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COLUMNUID,COLUMNUID); contentValues.put(COLUMNUSERNAME,COLUMNUSERNAME); contentValues.put(COLUMNEMAIL,COLUMNEMAIL); long result = db.insert(TABLEUSER,null ,contentValues); if(result == -1) return false; else return true; } /** * constructor of the database helper class * * @param context * @param name * @param factory * @param version */ public DatabaseHelper(Context context, String name, int version) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } } ,记录here

dangerouslySetInnerHTML

还讨论了here