mercredi 1 juillet 2015

creating SQLite database for android

public class MyApp extends Activity implements OnClickListener
{
    EditText editRollno,editName,editMarks,editsid,editlastname;
    Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
    SQLiteDatabase db;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editRollno=(EditText)findViewById(R.id.editRollno);
        editName=(EditText)findViewById(R.id.editName);
        editMarks=(EditText)findViewById(R.id.editMarks);
        editsid=(EditText) findViewById(R.id.editTextsid);
        editlastname=(EditText) findViewById(R.id.editTextlastname);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        btnModify=(Button)findViewById(R.id.btnModify);
        btnView=(Button)findViewById(R.id.btnView);
        btnViewAll=(Button)findViewById(R.id.btnViewAll);
        btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnView.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);
        btnShowInfo.setOnClickListener(this);
        **db=openOrCreateDatabase("CustomerInfo", Context.MODE_PRIVATE, null);**
        Toast.makeText(this,"connecting to db",Toast.LENGTH_SHORT).show();
        //db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");

db.execSQL("CREATE TABLE IF NOT EXISTS cnmame(id VARCHAR,sid VARCHAR,fname VARCHAR,gen VARCHAR);");

db.execSQL("CREATE TABLE IF NOT EXISTS lname(id VARCHAR,lnm VARCHAR);");

    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onClick(View view)
    {
        if(view==btnAdd)
        {
            if(editRollno.getText().toString().trim().length()==0||
               editName.getText().toString().trim().length()==0||
               editMarks.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter all values");
                return;
            }
            //db.execSQL("INSERT INTO cname VALUES('"+editRollno.getText()+"','"+editName.getText()+

            //"','"+editMarks.getText()+"');");

db.execSQL("INSERT INTO cname VALUES('"+editRollno.getText()+"','"+editsid.getText()+"','" +editName.getText()+"','"+editMarks.getText()+"');");

       }//if 
}//onCreate

//layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://ift.tt/nIICcg"
             android:id="@+id/myLayout"
             android:stretchColumns="0"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <TextView android:text="@string/title"
                  android:layout_x="110dp"
                  android:layout_y="10dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <TextView android:text="id"
                  android:layout_x="30dp"
                  android:layout_y="50dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editRollno"
                  android:inputType="number"     
                  android:layout_x="150dp"
                  android:layout_y="50dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <TextView android:text="@string/name"
                  android:layout_x="30dp"
                  android:layout_y="100dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editName"     
                  android:inputType="text"   
                  android:layout_x="150dp"
                  android:layout_y="100dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <TextView android:text="gender"
                  android:layout_x="30dp"
                  android:layout_y="150dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editMarks"    
                  android:inputType="number"     
                  android:layout_x="150dp"
                  android:layout_y="150dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnAdd"
                  android:text="@string/add"
                  android:layout_x="52dp"
                  android:layout_y="339dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnDelete"
                  android:text="@string/delete"
                  android:layout_x="170dp"
                  android:layout_y="337dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnModify"
                  android:text="@string/modify"
                  android:layout_x="45dp"
                  android:layout_y="395dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnView"
                  android:text="@string/view"
                  android:layout_x="168dp"
                  android:layout_y="400dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnViewAll"
                  android:text="@string/view_all"
                  android:layout_x="43dp"
                  android:layout_y="452dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnShowInfo"
                  android:text="@string/show_info"
                  android:layout_x="168dp"
                  android:layout_y="450dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="state id"
        android:id="@+id/textView"
        android:layout_x="25dp"
        android:layout_y="216dp" />

    <EditText
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:id="@+id/editTextsid"
        android:layout_x="141dp"
        android:layout_y="206dp" />

    <EditText
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:id="@+id/editTextlastname"
        android:layout_x="141dp"
        android:layout_y="264dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LAST NAME"
        android:id="@+id/textView2"
        android:layout_x="29dp"
        android:layout_y="283dp" />
</AbsoluteLayout>

SQLalchemy error for Sqlite DB when I compiled my python script with py2exe

I wrote a script manipualting sqlite db inside by using sqlalchemy interface. It works fine if I run this as a python script. however, when I compile it with py2exe and run the resulting .exe file, it raises eror and says the following;

File "sqlalchemy\engine\__init__.pyo", line 386, in create_engine
  File "sqlalchemy\engine\strategies.pyo", line 51, in create
  File "sqlalchemy\engine\url.pyo", line 131, in _get_entrypoint
  File "sqlalchemy\util\langhelpers.pyo", line 205, in load
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqlite

Any pointer for the solution ?

Syntax error in SQLite creating a table

I try to create a SQLite database. here my code below

public class DatabaseHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "AluShareDatabaseManager";

// Table Names
public static final String TABLE_BROADCASTLIST = "broadcastlist";
public static final String TABLE_BROADCAST_MEMBERLIST = "broadcast_memberlist";
public static final String TABLE_SETTINGS = "settings";
public static final String TABLE_CHAT = "chat";
public static final String TABLE_MESSAGELIST = "messagelist";
public static final String TABLE_RECEIVERLIST = "receiverlist";
public static final String TABLE_GROUP = "group";
public static final String TABLE_GROUP_MEMBERLIST = "group_memberlist";
public static final String TABLE_CONTACTLIST = "contactlist";

public static final String ID = "id";
// TABLE_BROADCASTLIST
public static final String BROADCASTLIST_ID = "id";
public static final String BROADCASTLIST_NAME = "name";
public static final String BROADCASTLIST_NOTE = "note";

// TABLE_BROADCAST_MEMBERLIST
public static final String BROADCAST_MEMBERLIST_ID = "broadcast_memberlist_id";
public static final String BROADCAST_MEMBER_ID = "broadcast_member_id";

// TABLE_SETTINGS
public static final String SETTINGS_ID = "id";
public static final String SETTINGS_LANGUAGE = "language";
public static final String SETTINGS_FONT = "font";
public static final String SETTINGS_FONTSIZE = "fontsize";
public static final String SETTINGS_BACKGROUND = "background";

// TABLE_CHAT
public static final String CHAT_ID = "id";
public static final String CHAT_NAME = "name";
public static final String CHAT_MUTED = "muted";
public static final String CHAT_TYPE = "type";

// TABLE_MESSAGELIST
public static final String MESSAGELIST_ID = "id";
public static final String MESSAGELIST_CHAT_ID = "messagelist_chat_id";
public static final String MESSAGE_TYPE = "type";
public static final String MESSAGE_CONTENT = "content";
public static final String MESSAGE_READ_FLAG = "read_flag";
public static final String MESSAGE_DATE = "date";
public static final String MESSAGE_RECEIVERLIST_ID = "receiverlist_id";

// TABLE_RECEIVERLIST
public static final String RECEIVERLIST_ID = "receiverlist_id";
public static final String RECEIVER_ID = "receiver_id";

// TABLE_GROUP
public static final String GROUP_ID = "id";
public static final String GROUP_CHAT_ID = "group_chat_id";
public static final String GROUP_NAME = "name";
public static final String GROUP_NOTE = "note";
public static final String GROUP_ADMIN = "admin";

// TABLE_GROUP_MEMBERLIST
public static final String GROUP_MEMBERLIST_ID = "group_memberlist_id";
public static final String GROUP_MEMBER_ID = "group_member_id";

// TABLE_CONTACTLIST
public static final String CONTACT_ID = "id";
public static final String CONTACT_CHAT_ID = "contact_chat_id";
public static final String CONTACT_NAME = "name";
public static final String CONTACT_NOTE = "note";
public static final String CONTACT_IGNORED = "ignored";

// Create TABLES
public static final String CREATE_TABLE_BROADCASTLIST = "CREATE TABLE "
        + TABLE_BROADCASTLIST + "(" + BROADCASTLIST_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + BROADCASTLIST_NAME
        + " TEXT, " + BROADCASTLIST_NOTE + " TEXT " + ")";
public static final String CREATE_TABLE_BROADCAST_MEMBERLIST = "CREATE TABLE "
        + TABLE_BROADCAST_MEMBERLIST
        + "("
        + ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + BROADCAST_MEMBERLIST_ID
        + " INTEGER, " + BROADCAST_MEMBER_ID + " TEXT " + ")";
public static final String CREATE_TABLE_SETTINGS = "CREATE TABLE "
        + TABLE_SETTINGS + "(" + SETTINGS_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SETTINGS_LANGUAGE
        + " TEXT, " + SETTINGS_FONT + " TEXT, " + SETTINGS_FONTSIZE
        + " TEXT, " + SETTINGS_BACKGROUND + " TEXT " + ")";
public static final String CREATE_TABLE_CHAT = "CREATE TABLE " + TABLE_CHAT
        + "(" + CHAT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + CHAT_NAME + " TEXT, " + CHAT_MUTED + " INTEGER, " // 0 (false) 1
                                                            // (true)
        + CHAT_TYPE + " TEXT " + ")";
public static final String CREATE_TABLE_MESSAGELIST = "CREATE TABLE "
        + TABLE_MESSAGELIST + "(" + MESSAGELIST_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MESSAGELIST_CHAT_ID
        + "INTEGER, " + MESSAGE_TYPE + " TEXT, " + MESSAGE_CONTENT
        + " TEXT, " + MESSAGE_READ_FLAG
        + " INTEGER, " // 0 (false) 1 (true)
        + MESSAGE_DATE + " INTEGER, " + MESSAGE_RECEIVERLIST_ID
        + "INTEGER " + ")";
public static final String CREATE_TABLE_RECEIVERLIST = "CREATE TABLE "
        + TABLE_RECEIVERLIST + "(" + ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + RECEIVERLIST_ID
        + " INTEGER, " + RECEIVER_ID + " TEXT " + ")";
public static final String CREATE_TABLE_GROUP = " CREATE TABLE "
        + TABLE_GROUP + "(" + GROUP_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + GROUP_CHAT_ID
        + " INTEGER, " + GROUP_NAME + " TEXT, " + GROUP_NOTE + " TEXT, "
        + GROUP_ADMIN + " TEXT " + ")";

public static final String CREATE_TABLE_GROUP_MEMBERLIST = "CREATE TABLE "
        + TABLE_GROUP_MEMBERLIST + "(" + ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + GROUP_MEMBERLIST_ID
        + " INTEGER, " + GROUP_MEMBER_ID + " TEXT " + ")";
public static final String CREATE_TABLE_CONTACTLIST = "CREATE TABLE "
        + TABLE_CONTACTLIST + "(" + CONTACT_ID + " TEXT PRIMARY KEY, "
        + CONTACT_CHAT_ID + "INTEGER, " + CONTACT_NAME + " TEXT, "
        + CONTACT_NOTE + " TEXT, " + CONTACT_IGNORED + " INTEGER " // 0
                                                                    // (false)
                                                                    // 1
                                                                    // (true)
        + ")";

/**
 * @param context
 * @param name
 * @param factory
 * @param version
 */
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/*
 * (non-Javadoc)
 * 
 * @see
 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
 * .SQLiteDatabase)
 */
@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(CREATE_TABLE_BROADCASTLIST);
    database.execSQL(CREATE_TABLE_BROADCAST_MEMBERLIST);
    database.execSQL(CREATE_TABLE_SETTINGS);
    database.execSQL(CREATE_TABLE_CHAT);
    database.execSQL(CREATE_TABLE_MESSAGELIST);
    database.execSQL(CREATE_TABLE_RECEIVERLIST);

    database.execSQL(CREATE_TABLE_GROUP_MEMBERLIST);
    database.execSQL(CREATE_TABLE_CONTACTLIST);
    database.execSQL(CREATE_TABLE_GROUP);
}

/*
 * (non-Javadoc)
 * 
 * @see
 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
 * .SQLiteDatabase, int, int)
 */
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
        int newVersion) {
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_BROADCASTLIST);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_BROADCAST_MEMBERLIST);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_CHAT);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGELIST);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_RECEIVERLIST);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_GROUP);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_GROUP_MEMBERLIST);
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTLIST);

    // recreate the tables
    onCreate(database);
}

}

When I run the application or a test in JUnit in log chat i get an error: With the Tag: SQLiteLog and Text (1) near "group": syntax error And in JUnit the same error:

android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: CREATE TABLE group(id INTEGER PRIMARY KEY AUTOINCREMENT, group_chat_id INTEGER, name TEXT, note TEXT, admin TEXT )

I wrote all group strings new to check if theire is a ' somewhere. When I remove database.execSQL(CREATE_TABLE_GROUP); the database is created as usual but i cant use the table group.

Maybe one of you see's my mistake :)

cant decrypt sqlite after SQLCipher

Im tying to Encrypt/Decrypt file with SQLCipher on mac.

this is what i do to encrypt db :

sqlite> ./sqlcipher test.db
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'Sef*DFyds98fhsh2';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;

and after that i try this to open encrypted DB :

sqlite> ./sqlcipher encrypted.db
sqlite> PRAGMA key = 'Sef*DFyds98fhsh2';
sqlite> select * from tables;

and it gives me this error :

Error: file is encrypted or is not a database

one thing is my original db file is 1.6 Megabyte but after encryption it turns into 226 KlioByte.

Common database framework between Android application and desktop tool

I have an Android app which uses a local SQLite database. The app can manipulate the data with ease using the native SQLite library.

I also have a desktop tool made in Java in order to perform some actions to the database effortlessly (like inserting data from the web as rows to the local db) using SQLite4Java. The problem is the fragmentation between the two applications and how could I keep the schema and most of the functionality common. What I did was that I created a module with all the common functionality like an interface for the schema, the database and a superclass the object where it would be mapped but even that left a lot of fragmentation and if I for example added a column lots of things would have to change.

My question is, is there a way to build a common framework for SQLite database management between Android and desktop or even if such a framework already exists?

Thus far I have found OrmLite which looks promising, is there any other frameworks with possibly better performance and preferably using an object oriented way?

Which Database is good for client / server application

I am new for the online application on vb.net. I want to create Client/Server Application in vb.net. Firstly I was trying MS Access for this task, unluckily MS Access not suitable for this task, because MS Acccess is not client/server database.

Now I need to go another option, So please give me your idea that Which is suitable and good database for client/server application.

column names as variables while creating table

Iam new to python sqlite, and I have a problem with create table query.

I need to create a table, but I have the column names of the table as a list.

columnslist = ["column1", "column2", "column3"]

Now, I have to create a table MyTable with the above columns. But the problem is, I won't know before hand how may columns are there in columnslist

Is it possible to create a table with the number of and name of columns given in columnslist and its syntax?