DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jonnie has posted 12 posts at DZone. View Full User Profile

Inserting Data Into SQLite Table

09.21.2008
| 6785 views |
  • submit to reddit
        For inserting into a SQLite table, use the code as follows:


//DB Connection
private var dbconn:SQLConnection;
//Query Statement
private var sqlQuery:SQLStatement;		
//Create Table Statement
private var sqlCreateTable:SQLStatement;
//Insert Statement
private var sqlInsert:SQLStatement;
//Import Statement
private var sqlImport:SQLStatement;

/**
 * Saves a post to the database by user and post
 * @param post The post to save
 * @param user The current user
 */			 
public function savePost( post:Post, user:User ):void
{		
	var query:String = "INSERT INTO posts (" + 
									"post_url," + 
									"post_hash," + 
									"post_desc," + 
									"post_tags," + 
									"post_time," + 
									"post_extended," + 
									"post_shared," + 
									"post_replace," + 
									"post_user)" + 
							"VALUES ( " + 
									":post_url," + 
									":post_hash," + 
									":post_desc," + 
									":post_tags," + 
									":post_time," + 
									":post_extended," + 
									":post_shared," + 
									":post_replace," +												
									":post_user)";

	sqlInsert = new SQLStatement();
	sqlInsert.sqlConnection = dbconn;
	sqlInsert.addEventListener( SQLEvent.RESULT, onSQLSave );
	sqlInsert.addEventListener( SQLErrorEvent.ERROR, onSQLError );				
	
	sqlInsert.text = query;
	sqlInsert.parameters[":post_url"] = post.post_url;
	sqlInsert.parameters[":post_hash"] = post.post_hash;
	sqlInsert.parameters[":post_desc"] = post.post_description;
	sqlInsert.parameters[":post_tags"] = post.post_tag;
	sqlInsert.parameters[":post_time"] = post.post_time;
	sqlInsert.parameters[":post_extended"] = post.post_extended;
	sqlInsert.parameters[":post_shared"] = post.post_shared;
	sqlInsert.parameters[":post_replace"] = post.post_replace;
	sqlInsert.parameters[":post_user"] = user.user_name;

	sqlInsert.execute();	
	
	trace( "Save post query: " + query );						
}